Home > Software engineering >  Function () does not exist error Laravel 9
Function () does not exist error Laravel 9

Time:10-26

I am trying to delete a post is Laravel and I get this error.

My web.php route looks like this:

Route::delete('/delete/{id}', [PostController::class], 'delete');

Controller method like this:

public function delete($id)
    {
        Post::destroy($id);
        return redirect()->route('home');
    }

I call it like this:

@foreach ($posts as $post)
    <h3>{{ $post->id }}</h3>
    <h1>{{ $post->name }}</h1>
    <h2>{{ $post->body }}</h2>

    <form action='/delete/{{ $post->id }}' method="post">
        @method('delete')
        @csrf
        <button type="submit" >
            <i >Delete</i>
        </button>
    </form>
@endforeach

Why is it giving me this Function () does not exist?

CodePudding user response:

Yes, just like Tim posted there was a typo in routing where it needed to be:

Route::delete('/delete/{id}', [PostController::class, 'delete']);

CodePudding user response:

put function name delete in Bracket such as

Route::delete('/delete/{id}', [PostController::class, 'delete']);

  • Related