【问题标题】:laravel: Call to a member function delete() on nulllaravel:在 null 上调用成员函数 delete()
【发布时间】:2016-11-05 00:40:49
【问题描述】:

当我尝试通过单击删除按钮将删除功能添加到帖子时出现此错误。 我哪里做错了?

在 PostController 中删除 post 函数:

public function getDeletePost($post_id)
{
    $post =Post::where('id',$post_id)->first();
    $post->delete();
    return redirect()->route('dashboard')->with(['message'=> 'Successfully deleted!!']);
} 

【问题讨论】:

  • 我现在在手机里,我建议直接删除而不是先删除,因为就我现在首先返回一个数组...
  • 方法first()返回Model|Builder|null(laravel.com/api/5.3/Illuminate/Database/Eloquent/…)
  • 好的,谢谢...@Joanquin Javi。我去看看。
  • 好的...我会检查一下...@Marek Skiba。谢谢。

标签: laravel


【解决方案1】:

$postobject 为空。也许您发送了错误的 $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!!']);
} 

【讨论】:

  • 谢谢,错误消失了。但是,仍然有一个麻烦。它显示帖子已被删除的消息,但事实并非如此。它仍在我的仪表板和数据库中。
  • 编辑后如果 post_id 错误则显示不同的消息。
  • 谢谢@Victor Ya,我做到了。它也有效,但是,我不明白问题出在哪里。 “users”表包含“id”列,它将“posts”表与“posts”表的“user_id”列链接起来。我尝试了上面的代码,它显示了“错误的 ID”,然后我用“user_id”编辑了“id”,再次显示相同的消息。这里有什么问题?
【解决方案2】:

你好像没有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!!']);
}

或者开始使用Route Model Binding,然后你就不需要关心Postid = $post_id是否存在:

所以首先你需要在RouteServiceProvider::boot中添加绑定:

$router->model('post', 'App\Post');

那么在路线中你需要改变这个:

Route::post('post/{post}/delete', [
    'as' => 'post.delete', 'uses' => 'PostController@getDeletePost'
]);

然后你的控制器看起来像这样:

public function getDeletePost(Post $post)
{
    $post->delete();
    return redirect()->route('dashboard')->with(['message'=> 'Successfully deleted!!']);
}

如果您仍然有问题,您应该向我们展示您如何构建向控制器发送请求的 POST 表单。

【讨论】:

    【解决方案3】:

    您的 $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!!']);
    } 
    

    【讨论】:

    • 谢谢,但是...不,它仍然显示相同的错误。
    • 其实我试过@victor 的方法。它确实奏效了。它现在显示成功删除的消息,但没有从我的仪表板和数据库中删除帖子。
    【解决方案4】:

    您雄辩的查询没有返回结果,因此 $post 变量为空,导致错误。

    如果没有找到具有提供的 id 的记录,则使用 findOrFail 将引发异常。

    $post = Post::findOrFail($id);
    $post->delete();
    return redirect()->route('dashboard')->with(['message'=> 'Successfully deleted!!']);
    

    如果抛出 ModelNotFound 异常,则表示数据库中不存在具有提供的 Id 的 Post 记录。

    【讨论】:

      猜你喜欢
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-04
      • 2016-06-29
      • 2018-04-03
      • 2021-10-06
      • 2019-06-17
      相关资源
      最近更新 更多