laravel:在 null 上调用成员函数 delete()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40421415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel: Call to a member function delete() on null
提问by Learning_to_solve_problems
I got this error when I tried to add a feature of delete, to the posts just by clicking on the delete button. Where am I doing wrong?
当我尝试通过单击删除按钮向帖子添加删除功能时出现此错误。我哪里做错了?
Delete post function in PostController:
删除 PostController 中的 post 函数:
public function getDeletePost($post_id)
{
$post =Post::where('id',$post_id)->first();
$post->delete();
return redirect()->route('dashboard')->with(['message'=> 'Successfully deleted!!']);
}
回答by Victor
$post
object is null. Maybe you are sending a wrong $post_id. If you check that the post exists before delete you avoid that error.
$post
对象为空。也许您发送了错误的 $post_id。如果您在删除之前检查帖子是否存在,则可以避免该错误。
public function getDeletePost($post_id)
{
$post =Post::where('id',$post_id)->first();
if ($post != null) {
$post->delete();
return redirect()->route('dashboard')->with(['message'=> 'Successfully deleted!!']);
}
return redirect()->route('dashboard')->with(['message'=> 'Wrong ID!!']);
}
回答by Marek Skiba
It looks like you don't have Post
where id = $post_id
, you can try with firstOrFail
method:
看起来你没有Post
where id = $post_id
,你可以尝试使用firstOrFail
方法:
public function getDeletePost($post_id)
{
$post =Post::where('id',$post_id)->firstOrFail();
$post->delete();
return redirect()->route('dashboard')->with(['message'=> 'Successfully deleted!!']);
}
or start using Route Model Bindingand then you don't need care about that Post
with id = $post_id
exist or not:
或者开始使用路由模型绑定,然后你不需要关心这个Post
与id = $post_id
存在或不:
So first you need add binding in the RouteServiceProvider::boot
:
所以首先你需要在 中添加绑定RouteServiceProvider::boot
:
$router->model('post', 'App\Post');
then in route you need change to this:
然后在路线中,您需要更改为:
Route::post('post/{post}/delete', [
'as' => 'post.delete', 'uses' => 'PostController@getDeletePost'
]);
and then your controller looks like this:
然后你的控制器看起来像这样:
public function getDeletePost(Post $post)
{
$post->delete();
return redirect()->route('dashboard')->with(['message'=> 'Successfully deleted!!']);
}
If you have still problem, you should show us how you build POST form that send request to the controller.
如果您仍然有问题,您应该向我们展示您如何构建向控制器发送请求的 POST 表单。
回答by ptorsson
There is a chance that your $post variable is null/undefined.
您的 $post 变量有可能为空/未定义。
public function getDeletePost($post_id) {
try {
$post = Post::where('id',$post_id)->first();
} catch (ModelNotFoundException $e) {
return redirect()->route('dashboard')->with(['message'=> 'Failed']);
}
$post->delete();
return redirect()->route('dashboard')->with(['message'=> 'Successfully deleted!!']);
}
回答by Saurabh
You eloquent query is not returning a result so the $post variable is empty which is causing the error.
您雄辩的查询没有返回结果,因此 $post 变量为空,这导致了错误。
Instead use findOrFail which will throw exception if no record is found with provided id.
而是使用 findOrFail 如果没有找到具有提供的 id 的记录,它将抛出异常。
$post = Post::findOrFail($id);
$post->delete();
return redirect()->route('dashboard')->with(['message'=> 'Successfully deleted!!']);
If ModelNotFound exception is thrown then that means Post record does not exist in database with the provided Id.
如果抛出 ModelNotFound 异常,则意味着具有提供的 ID 的数据库中不存在 Post 记录。