Eloquent query where column is in another table

You can use Laravel's DB class to perform joins on two or more tables, following is how your query will be executed in laravel:

$users = DB::table('posts')
        ->leftJoin('likes', 'posts.id', '=', 'likes.post_id')
        ->select('posts.*', 'likes.*')
        ->where('likes.user_id', '=', '2')
        ->orderBy('likes.created_at', 'desc')
        ->get();

Don't forget to use DB class on the top of your controller;

If you want to do it with eloquent, you should do the follwing:

$result = Post::whereHas('likes', function ($q) use($user_id)
                {
                   $q->where('user_id', $user_id);
                })
                  ->orderBy('likes.created_at')
                  ->get();

This should work:

$userId = //however you get the userid here.

$posts = Post::whereHas('likes', function ($q) use ($userId) {
    $q->where('user_id', $user_id);
})->get();