LARAVEL: How to fetch id dynamically in a query builder?



PHP Snippet 1:

$datauser = DB::table('users')
->join('activitates','users.id','=','activitates.user_id')
->join('taga_cars','taga_cars.id','=','activitates.tagacar_id')
->join('clients','users.id','=','clients.user_id')
->where('users.id','=',$id)
->select('users.*','activitates.*','taga_cars.model','taga_cars.id','clients.name')
->get();

return response()->json($datauser);

PHP Snippet 2:

public function showuser($id)
{
    $getUserByID = User::findOrFail($id); //not used
    $userData = DB::table('users')
    ->join('activitates','users.id','=','activitates.user_id')
    ->join('taga_cars','taga_cars.id','=','activitates.tagacar_id')
    ->join('clients','users.id','=','clients.user_id')
    ->where('users.id','=',$id)
    ->select('users.*','activitates.*','taga_cars.model','taga_cars.id','clients.name')
    ->get();
   
    return response()->json($userData);
}

PHP Snippet 3:

public function showuser($id)
{
   $userData = User::where('id', $id)->with(['activitates','taga_cars','clients'])->first();
   return response()->json($userData);
}