Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jmurphy1267's avatar

No query results for model [App\Like]

I am trying to develop a liking ability of posts for my website. This method detects if a user already liked the post and if so deletes his like. It will always create the like but the majority of the time I get this error when I try to unlike the post. When I try to unlike the post I get this error No query results for model [App\Like].

public function store(Request $request)
{
    $liked = Like::where(function ($query) use ($request){
        $query->where('user_id',$request->user()->id);
        $query->where('likable_id',$request->post_id);
    })->first();
    $post = Post::findOrFail($request->post_id);

    if ($liked){
        Log::info($liked->id);
        Like::destroy($liked->id);
        Redis::decr('post::'.$request->post_id.'::likes');
        broadcast(new PostDisliked($post,$liked))->toOthers();
        return $liked;
    }
    
    $like = $post->likes()->create([
        'user_id' =>  $request->user()->id,
    ]);

    Redis::incr('post::'.$post->id.'::likes');
    broadcast(new PostLiked($post,$like))->toOthers();

    return $like;
}

I have no idea whats going on might just need a second eye.

0 likes
1 reply
D9705996's avatar

You could save yourself some effort and avoid reinventing the wheel using an existing package like

https://laravel-news.com/laravel-love

If this isnt an option let me know and I'll take a look at your code but would need to see you model/relationships involved.

Please or to participate in this conversation.