【发布时间】: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