我試圖做一個社交網站類型的項目,並試圖刪除一個帖子,當用戶登錄時,他只會被允許刪除他的帖子,並且可以只喜歡和不喜歡別人的帖子。 但是,當我試圖刪除它:它會顯示錯誤 -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 - 做到了,請立即檢查。 –
謝謝。我已經發布了。 –