Laravel:尝试获取非对象的属性

Laravel:尝试获取非对象的属性,laravel,Laravel,我试着做一个社交网站类型的项目,并试图在用户登录时删除一篇帖子,他只能删除自己的帖子,只能喜欢和不喜欢别人的帖子。 但是,当我试图删除它时:它显示错误- Trying to get property of non-object 在这次编辑之前。当任何人能够删除任何人的帖子时,使用以下代码: public function getDeletePost($post_id){ $post = Post::where('user_id',$post_id)->first();

我试着做一个社交网站类型的项目,并试图在用户登录时删除一篇帖子,他只能删除自己的帖子,只能喜欢和不喜欢别人的帖子。 但是,当我试图删除它时:它显示错误-

Trying to get property of non-object
在这次编辑之前。当任何人能够删除任何人的帖子时,使用以下代码:

public function getDeletePost($post_id){

    $post = Post::where('user_id',$post_id)->first();

    if (Auth::user() != $post->user) {
        return redirect()->back();
    }

    if ($post != null) {
        $post->delete();
        return redirect()->route('dashboard')->with(['message'=> 'Successfully deleted!!']);
    }

    return redirect()->route('dashboard')->with(['message'=> 'Wrong ID!!']);
}
每次只显示:“错误ID!!”,不要删除帖子

用户表的列:id、电子邮件、密码 posts表的列:id、created\u at、updated\u at、body、user\u id

并且,posts表的user_id与users表的id链接

添加了dd(Auth::user(),$post):

这里怎么了?

包含删除按钮的部分代码:来自dashboard.blade.php:

<div class="interaction">
                <a href="#">Like</a> |
                <a href="#">Dislike</a> 
                @if(Auth::user() == $post->user)
                |
                <a href="#">Edit</a> |
                <a href="{{route('post.delete',['post_id=>$post->id'])}}">Delete</a> 
                @endif
            </div>

|
@如果(Auth::user()=$post->user)
|
|
@恩迪夫

我建议您使用
firstOrFail()
而不是使用
first()
作为:

$post = Post::where('user_id',$post_id)->firstOrFail();
如果使用
firstOrFail()
,它将确认
$post
对象是模型对象,而不是
null

firstOrFail()
方法将检索查询的第一个结果;但是,如果未找到任何结果,将引发Illumb\Database\Eloquent\ModelNotFoundException。因此,如果进行了
检查,则永远不需要

编辑

 public function getDeletePost($post_id)
    {
        $post = Post::find($post_id);
        if ($post) {
            if (auth()->user()->id != $post->user_id) {
                return redirect()->back();
            }
            $post->delete();
            return redirect()->route('dashboard')->with(['message' => 'Successfully deleted!!']);
        }
        return redirect()->route('dashboard')->with(['message' => 'Wrong ID!!']);

    }

您试图从
null
获取数据,因此会导致错误。只需添加
$post!=如果
为空,则返回第一个
,它将按预期工作:

public function getDeletePost($post_id){

    $post = Post::where('user_id',$post_id)->first();

    if ($post != null && Auth::user() != $post->user) {
        return redirect()->back();
    }

    if ($post != null) {
        $post->delete();
        return redirect()->route('dashboard')->with(['message'=> 'Successfully deleted!!']);
    }

    return redirect()->route('dashboard')->with(['message'=> 'Wrong ID!!']);
}

请将其放在第一个
if
之前,并向我们显示输出是什么
dd(Auth::user(),$post)@Alexey Mezenin-成功了,请立即查看。谢谢。我已经发布了asnwer。但是,我正在使用if语句进行检查。所以,我需要检查两次?或者我应该删除您正在使用的第一个
if
语句中的if语句,
$post->user
。因此,这可能会导致问题。JFI-为什么要查询变量名为
$paost\u id
user\u id
列。是不是应该是
Post::where('id',$Post\u id)
。是的,我也这么认为。试过了。但是,这也显示了相同的错误。然后,您必须确保表中有
Post
,并且
id
$Post\u id
。我看这才是可能的问题。或者您可以使用
toSql()
函数检查您的SQL查询。谢谢。尝试了它,但显示了相同的错误:尝试获取非对象的属性。您可以尝试
dd($post)
并查看是否有一行具有该post id并检查数据库的交叉引用吗?@shoieb0101完成了,请再次检查我的问题。它显示为空。@Learning_to_solution_problems您能检查您传递给
方法
$post_id
是否存在于
posts
表中吗?不,它包含列id。也尝试过了。但是,显示了相同的错误。我再查一遍。谢谢:)谢谢,现在没有显示错误。但它也造成了我在问题中提到的同样的问题。它说的是“错误的id!!”,是什么导致了这个问题?这意味着表中没有
user\u id=$Post\u id
Post
。可能您想搜索
'id'=$post\u id
而不是
'user\u id'=$post\u id
?我尝试使用$id而不是$user\u id,但显示相同的错误请显示您的表结构和
dd($post\u id)输出然后我就可以帮你了。dd($post\u id):输出-“post\u id=>$post->id”
public function getDeletePost($post_id){

    $post = Post::where('user_id',$post_id)->first();

    if ($post != null && Auth::user() != $post->user) {
        return redirect()->back();
    }

    if ($post != null) {
        $post->delete();
        return redirect()->route('dashboard')->with(['message'=> 'Successfully deleted!!']);
    }

    return redirect()->route('dashboard')->with(['message'=> 'Wrong ID!!']);
}