Laravel 控制器 return 模型实例而不是数据内容

Laravel controller return model instance instead data content

我有这条路线:

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

在我的控制器中我有

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

通常我必须有各自id的数据吧?但是我得到了模型的空实例

要工作,您需要将路由参数从 id 更改为 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');

阅读此处:https://laravel.com/docs/8.x/routing#implicit-binding