Question

I want to perform Laravel4 Query Builder Left Join. Here is my code:

$settings = DB::table('settings')
 ->leftJoin('users', 'settings.user_id', '=', 'users.user_id')->get();

With this part of code I get all fields from both database table. I want to get all fields from settings table but just country and currency field from users table. Can someone help me solve this problem ?

Was it helpful?

Solution

Instead of using ->get() which is used to get all columns you need to use ->select() to say which columns you want and then use get()

$settings = DB::table('settings')
->leftJoin('users', 'settings.user_id', '=', 'users.user_id')
->select('settings.*', 'users.country', 'users.currency')->get();

OTHER TIPS

You may try this, pass fields to select in get() as array:

$settings = DB::table('settings')
              ->leftJoin('users', 'settings.user_id', '=', 'users.user_id')
              ->get(array('settings.*', 'users.country', 'users.currency'));

Using ->select('settings.*', 'users.country', 'users.currency') you still need to call get() at the end, like this:

$settings = DB::table('settings')
              ->leftJoin('users', 'settings.user_id', '=', 'users.user_id')
              ->select('settings.*', 'users.country', 'users.currency')->get();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top