我试图做一个社交网站类型的项目,并试图删除一个帖子,当用户登录时,他只会被允许删除他的帖子,并且可以只喜欢和不喜欢别人的帖子。 但是,当我试图删除它:它会显示错误 -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>
请第一个'if'之前把这个和我们展示输出是什么'DD(AUTH ::用户(),$后); ' –
@Alexey Mezenin - 做到了,请立即检查。 –
谢谢。我已经发布了。 –