我试图通过id删除单个记录.相反,它会删除该表中的所有记录.
这是我的代码:
视图
<form role="form" action="{{ route('status.delete', ['statusId' => $status->id]) }}" method="post">
<button type="submit" class="btn btn-default"><i class="fa fa-times"></i> Delete</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
路线
Route::post('/status/{statusId}/delete', [
'uses' => '\Dashboard\Http\Controllers\StatusController@deleteStatus',
'as' => 'status.delete',
'middleware' => ['auth'],
]);
调节器
public function deleteStatus(Request $request, $statusId)
{
Auth::user()->statuses()->delete($statusId);
return redirect()->route('home')->with('success', 'Post deleted.');
}
注意:当我dd($statusId)时,它确实为我正在删除的状态提供了正确的ID.这部分确实有效.
解决方法:
不幸的是,Eloquent构建器不支持将id传递给delete.
相反,你必须首先找到模型,然后在其上调用delete:
$request->user()->statuses()->findOrFail($statusId)->delete();