2016-11-07 86 views
1

我试图做一个社交网站类型的项目,并试图删除一个帖子,当用户登录时,他只会被允许删除他的帖子,并且可以只喜欢和不喜欢别人的帖子。 但是,当我试图删除它:它会显示错误 -Laravel:试图获取非对象的财产

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,电子邮件,密码 职位表中的列:ID,created_at,的updated_at,身体邀请,USER_ID

而且,员额表链接的user_id与用户表的ID。

新增DD(AUTH ::用户(),$后):

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> 
+0

请第一个'if'之前把这个和我们展示输出是什么'DD(AUTH ::用户(),$后); ' –

+0

@Alexey Mezenin - 做到了,请立即检查。 –

+0

谢谢。我已经发布了。 –

回答

1

你试图从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!!']); 
} 
+0

谢谢,它现在没有显示错误。但它导致了我在我的问题中提到的同样的问题。它说“错误的编号!!”,是什么导致了这个问题? –

+0

这意味着你在表格中没有带'user_id = $ post_id'的'Post'。也许你想搜索''id'== $ post_id'而不是''user_id'== $ post_id'? –

+0

我尝试使用$ id而不是$ user_id,但显示相同的错误 –

0

我建议你使用firstOrFail()而不是使用first()为:如果你使用firstOrFail()然后

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

它会确认您的$post对象是模型对象,而不是null

firstOrFail()方法将检索查询的第一个结果;但是,如果未找到任何结果,则会引发Illuminate \ Database \ Eloquent \ ModelNotFoundException。所以你不必要求if检查。

+0

但是,我正在使用if语句来检查它。那么,我需要检查两次?或者我应该删除if语句 –

+0

在您使用的第一个'if'语句中,'$ post-> user'。所以这可能会导致问题。 –

+0

好吧,我会检查它。 –

0

编辑

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

    } 
+0

谢谢。尝试它,但显示相同的错误:试图获得非对象 –

+0

的属性,你可以请尝试'dd($ post)',看看是否有一行与该帖子ID,并检查数据库的交叉引用? – shoieb0101

+0

@Learning_to_solve_problems代码编辑 – shoieb0101