'Join subqueries in Laravel Eloquent

I want to join subqueries in Laravel Eloquent using the following code. Everything is working fine. But i want the results in subqueries.

  $tags = Snippet::
     join('snippettags', 'snippets.id', '=', 'snippettags.snippet_id')
    ->join('users','snippets.user_id','=','users.id')
    ->where("snippettags.tag_id",1)
    ->paginate(2, ['snippets.id','snippets.name','users.id as user_id','users.name as user_name']);  
    $data = array(
        'status'=>200,
        'title'=>"All Tags",
        'description'=>"All Tags",
        'tags'=>$tags,  
    );
    return $data;

getting this result

{
"id": 209,
"name": "Taken 3 all around",
"user_id": 2,
"user_name": "John Doe"
}

But I want the following result

[{
"id": 209,
"name": "Taken 3 all around",
"users":[{
  "user_id": 1,
 "user_name": "John Doe"
}]

}]



Solution 1:[1]

It works for me

$tags = Snippet::query()
    ->select('snippets.id','snippets.name','snippets.user_id')
    ->join('snippettags', 'snippets.id', '=', 'snippettags.snippet_id')
    ->with('user')
   ->where("snippettags.tag_id",1)
   ->paginate(2); 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Upasana Chauhan