Check if post is liked by user. Laravel
<p>I want to check if post was liked by a authenticated user when I display all posts of the users. This is my approach to this right now:</p>

<pre><code>public function index(){
    $posts = Post::orderBy('created_at', 'desc')-&gt;get();
    $user = Auth::user();
    $liked = Post::with('user')
        -&gt;whereHas('like', function ($query) use ($user) {
            $query-&gt;where('user_id', '=', $user-&gt;id);
        })-&gt;get();
    return view('index', compact('posts', 'liked'));
}
</code></pre>

<p>And when I do this in HTML:</p>

<pre><code>@if($liked)
    &lt;button class="[ btn btn-primary ]"&gt;
    &lt;i class="fa fa-thumbs-o-up" aria-hidden="true"&gt;&lt;/i&gt; You like this&lt;/button&gt;
@else
    &lt;a href="/patinka/{{$p-&gt;id}}" type="button" class="[ btn btn-default ]"&gt;
    &lt;i class="fa fa-thumbs-o-up" aria-hidden="true"&gt;&lt;/i&gt; Like&lt;/a&gt;
@endif
</code></pre>

<p>I always get "You like this" even though I did not liked that post. Can someone tell me whats wrong with this code ? </p>