Michel Gisenaël
Michel Gisenaël

Reputation: 351

Laravel controller return model instance instead data content

i have this route:

Route::group(['prefix' => 'comments', 'middleware' => ['auth:api']], function(){
    Route::delete('/delete/{id}', [CommentController::class, 'destroy'])->name('comment.delete');
});

in my controller i have

public function destroy(Comments $comments)
    {
        dd($comments);
    }

normally i must have the data of respective of the id right? but instead i got empty instance of the model like this

Upvotes: 0

Views: 450

Answers (1)

John Lobo
John Lobo

Reputation: 15319

To work you need to change route parameter from id to comments

Laravel automatically resolves Eloquent models defined in routes or controller actions whose type-hinted variable names match a route segment name.

Route::delete('/delete/{comments}', [CommentController::class, 'destroy'])->name('comment.delete');

Read here : https://laravel.com/docs/8.x/routing#implicit-binding.

Upvotes: 1

Related Questions