0

I've got an error in laravel named

Target class [App\Http\Controllers\PostController] does not exist

The code below is from my PostsController

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

use Illuminate\Support\Facades\Auth;

use Illuminate\Support\Facades\DB;

public function postCreate(Request $request)
{
   $id = Auth::id();
   $gebrnaam = Auth::name();
   $post = new Post();
   $image = $request->file('image')->getClientOriginalName();
   $post->image_path = $image;
   $request->image->move(public_path('img'), $image);
   $post->Titel = $request->input('txtTitle');
   $post->Inleiding = $request->input('txtinleiding');
   $post->Inhoud = $request->input('txtinhoud');
   $post->user_id = $id;
   $post->userNaam = $gebrnaam;
   $post->save();
}

And this is the code of my web.php


use Illuminate\Support\Facades\Route;

use App\Http\Controllers\PostController;

Route::get('/', function () {

    return view('welcome');
});

Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {

     return view('dashboard');
 })->name('dashboard');

Route::get('/', [PostController::class, 'getpost']);
Route::post('/', [PostController::class, 'postCreate']);
3
  • Does PostController exist in the app/Http/Controllers directory and do you have the correct spelling? Commented Dec 4, 2021 at 20:52
  • yes it does and it has the correct spelling so idk what i am doing wrong Commented Dec 4, 2021 at 21:43
  • 2
    Welcome to SO ... PostsController != PostController Commented Dec 4, 2021 at 21:59

1 Answer 1

1

You haven't declared the class in your PostContoller so the Laravel is throwing an exception as it is not able to find the controller class.

class PostController extends Controller
 {
 //
 }

Wrap your code in class PostController so as follows:

<?php 

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

use Illuminate\Support\Facades\Auth;

use Illuminate\Support\Facades\DB;

use App\Http\Controllers\Controller;

 class PostController extends Controller{ 
    
    public function postCreate(Request $request)
    {
       $id = Auth::id();
       $gebrnaam = Auth::name();
       $post = new Post();
       $image = $request->file('image')->getClientOriginalName();
       $post->image_path = $image;
       $request->image->move(public_path('img'), $image);
       $post->Titel = $request->input('txtTitle');
       $post->Inleiding = $request->input('txtinleiding');
       $post->Inhoud = $request->input('txtinhoud');
       $post->user_id = $id;
       $post->userNaam = $gebrnaam;
       $post->save();
      }

    }

For reference: https://laravel.com/docs/8.x/controllers#basic-controllers

Sign up to request clarification or add additional context in comments.

3 Comments

sorry that was the only part i didnt copy over to stackoverflow my bad, i alreadey have it like the example from above... so that was not the mistake that i made
Is the controller file named PostController or PostController in the directory?
the 2 options you just asked are the same ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.