laravel:在 null 上调用成员函数 delete()

标签 laravel

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

删除 PostController 中的 post 函数:

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

最佳答案

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

关于laravel:在 null 上调用成员函数 delete(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40421415/

相关文章:

javascript - 单击按钮重定向到 laravel 路由链接

Laravel FFMpeg - FFMpeg 无法就地编辑现有文件

php - 如何将文件直接上传到Cloudinary而不先将其保存在公共(public)文件夹中

laravel - Blade : extending a template in multiple subviews

php - 如何在 jeroennoten admin-lte laravel 上编辑和更改侧边栏菜单项?

laravel - 重命名 Windows IIS 服务器上默认网站内的网站

php - Eloquent/PHP 范围(从/到)

php - Laravel 特定时间后触发事件或监听器

php - 无法使用 Laravel 的身份验证

php - 如何从闭包中终止 Eloquent 分块