Laravel: Trying to get property of non-object
<p>I tried to make a social networking site type of project and tried to delete a post when the user is logged in, and he will only be allowed to delete his posts, and can only like and dislike other's posts. 
But, when I tried to delete it: it shows error - </p>

<pre><code>Trying to get property of non-object
</code></pre>

<p>And, before this editing. when anyone was able to delete anyone's post, with the code:</p>

<pre><code>public function getDeletePost($post_id){

    $post = Post::where('user_id',$post_id)-&gt;first();

    if (Auth::user() != $post-&gt;user) {
        return redirect()-&gt;back();
    }

    if ($post != null) {
        $post-&gt;delete();
        return redirect()-&gt;route('dashboard')-&gt;with(['message'=&gt; 'Successfully deleted!!']);
    }

    return redirect()-&gt;route('dashboard')-&gt;with(['message'=&gt; 'Wrong ID!!']);
}
</code></pre>

<p>It only shows: "Wrong ID!!" every time and do not delete the post. </p>

<p>The columns of users table: id, email, password
The columns of posts table: id, created_at,updated_at,body,user_id</p>

<p>And, user_id of posts table links with the id of users table.</p>

<p>Added dd(Auth::user(), $post):</p>

<pre><code>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
</code></pre>

<p><strong>What is wrong here?</strong></p>

<p>Part of code containing delete button: from dashboard.blade.php: </p>

<pre><code>&lt;div class="interaction"&gt;
                &lt;a href="#"&gt;Like&lt;/a&gt; |
                &lt;a href="#"&gt;Dislike&lt;/a&gt; 
                @if(Auth::user() == $post-&gt;user)
                |
                &lt;a href="#"&gt;Edit&lt;/a&gt; |
                &lt;a href="{{route('post.delete',['post_id=&gt;$post-&gt;id'])}}"&gt;Delete&lt;/a&gt; 
                @endif
            &lt;/div&gt;
</code></pre>