2016-11-07 85 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