문제

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 ?

도움이 되었습니까?

해결책

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();

다른 팁

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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top