【问题标题】:Laravel: Trying to get property of non-objectLaravel:试图获取非对象的属性
【发布时间】:2017-03-20 14:41:33
【问题描述】:

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

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!!”每次都不要删除帖子。

users表的列:id、email、password 帖子表的列:id、created_at、updated_at、body、user_id

而且,posts 表的 user_id 与 users 表的 id 链接。

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

User {#183 ▼
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:7 [▶]
#original: array:7 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
+exists: true
+wasRecentlyCreated: false
}
null

这里有什么问题?

包含删除按钮的部分代码:来自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>

【问题讨论】:

  • 请把它放在第一个if之前,告诉我们输出是什么dd(Auth::user(), $post);
  • @Alexey Mezenin - 做到了,请立即查看。
  • 谢谢。我已经发布了答案。

标签: laravel


【解决方案1】:

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

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

如果您使用firstOrFail(),那么它将确认您的$post 对象是模型对象,而不是null

firstOrFail() 方法将检索查询的第一个结果;但是,如果没有找到结果,则会抛出 Illuminate\Database\Eloquent\ModelNotFoundException。所以你永远不需要if检查。

【讨论】:

  • 但是,我正在使用 if 语句检查它。所以,我需要检查两次吗?或者我应该删除 if 语句
  • 在您使用的第一个 if 语句中,$post-&gt;user。所以这可能会导致问题。
  • JFI - 为什么要查询变量名为 $paost_iduser_id 列。是不是应该是Post::where('id',$post_id).
  • 是的,我是这么认为的。试过了。但是,这也显示了同样的错误。
  • 那么你必须确保Postid$post_id 的表中。我只能看到这可能是可能的问题。或者您可以使用toSql() 函数检查您的 SQL 查询。
【解决方案2】:

编辑

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

    }

【讨论】:

  • 谢谢。试过了,但显示同样的错误:尝试获取非对象的属性
  • 您可以试试dd($post) 看看是否有一行具有该帖子ID 并检查数据库以进行交叉引用?
  • @shoieb0101-做到了,请再次检查我的问题。它显示为空。
  • @Learning_to_solve_problems 您能否检查您传递给method$post_id 是否存在于posts 表中?
  • 不,它包含列 ID。也试过了。但是,显示相同的错误。我会再检查一次。谢谢你:)
【解决方案3】:

您正试图从null 获取数据,因此会导致错误。只需将$post != null 添加到第一个if,它就会按您的预期工作:

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

【讨论】:

  • 谢谢,现在没有显示那个错误了。但这导致了我在问题中提到的相同问题。它说“错误的ID!”,是什么导致了这个问题?
  • 这意味着您在表格中没有Postuser_id = $post_id。也许您想搜索'id' == $post_id 而不是'user_id' == $post_id
  • 我尝试使用 $id 而不是 $user_id,但显示同样的错误
  • 请显示您的表结构和dd($post_id); 输出,然后我可以帮助您。
  • dd($post_id): 输出 -"post_id=>$post->id"
猜你喜欢
  • 2015-09-22
  • 2014-03-25
  • 2015-01-25
  • 2014-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多