Home > Software design >  Laravel controller return model instance instead data content
Laravel controller return model instance instead data content

Time:12-12

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

CodePudding user response:

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.

  • Related