Laravel Eloquent: how to get only specific columns from joined tables

I have two joined tables in Eloquent, namely themes and users.

theme model:

public function user() { return $this->belongs_to('User'); } 

user model:

 public function themes() { return $this->has_many('Theme'); } 

My Eloquent api call is as follows:

 return Response::eloquent(Theme::with('user')->get()); 

Returns all columns from the topic (this is fine) and all columns from the user (not really). I only need the "username" column from the user model, how can I limit this query?

+83
php eloquent laravel
Feb 06 '13 at
source share
14 answers

Modify your model to indicate which columns you want to select:

 public function user() { return $this->belongs_to('User')->select(array('id', 'username')); } 

And don't forget to include the column you are entering.

+94
Apr 25 '13 at 2:37
source share

For Laravel> = 5.2

Use the method -> pluck ()

 $roles = DB::table('roles')->pluck('title'); 

If you want to get an array containing the values ​​of a single column, you can use the pluck method




For Laravel <= 5.1

Use the method -> lists ()

 $roles = DB::table('roles')->lists('title'); 

This method will return an array of role names. You can also specify your own key column for the returned array:

+32
Oct 13 '13 at 12:05
source share

You can specify an array of fields in the get parameter like this:

 return Response::eloquent(Theme::with('user')->get(array('user.username')); 

UPDATE (for Laravel 5.2) From docs you can do this:

 $response = DB::table('themes') ->select('themes.*', 'users.username') ->join('users', 'users.id', '=', 'themes.user_id') ->get(); 
+25
Jun 11 '13 at 5:46
source share

I know you ask for Eloquent, but you can do it with Fluent Query Builder

 $data = DB::table('themes') ->join('users', 'users.id', '=', 'themes.user_id') ->get(array('themes.*', 'users.username')); 
+20
Feb 06 '13 at 12:22
source share

Another option is to use the $hidden property in the model to hide columns that you do not want to display. You can define this property on the fly or set default values ​​for your model.

 public static $hidden = array('password'); 

Now the user password will be hidden when returning a JSON response.

You can also set it on the fly in a similar way.

 User::$hidden = array('password'); 
+10
Apr 25 '13 at 4:28
source share

This is how i do it

 $posts = Post::with(['category' => function($query){ $query->select('id', 'name'); }])->get(); 

User first answer2317976 did not work for me, I am using laravel 5.1

+10
Feb 16 '16 at 15:46
source share

Paginated Use

 $data = DB::table('themes') ->join('users', 'users.id', '=', 'themes.user_id') ->select('themes.*', 'users.username') ->paginate(6); 
+8
Jun 30 '15 at 13:06
source share

user2317976 introduced a great static way to select columns of related tables.

Here is a dynamic trick I found so that you can get whatever you want when using the model:

 return Response::eloquent(Theme::with(array('user' => function ($q) { $q->addSelect(array('id','username')) }))->get(); 

I just found that this trick also works well with load (). It is very convenient.

 $queriedTheme->load(array('user'=>function($q){$q->addSelect(..)}); 

Make sure you also include the key of the target table, otherwise it will not be able to find it.

+5
Oct 22 '14 at 2:52
source share

This way:

 Post::with(array('user'=>function($query){ $query->select('id','username'); }))->get(); 
+5
Mar 03 '16 at 5:46
source share

I know this is an old question, but if you are creating an API as the author of the question, use output transformers to perform such tasks.

Transofrmer is the layer between the actual result of the database query and the controller. This makes it easy to control and change what will be displayed to the user or consumer of the API.

I recommend Fractal as a solid foundation for your output conversion level. You can read the documentation here .

+4
03 Oct '14 at 9:59
source share

In Laravel 4, you can hide certain fields from returning by adding the following to your model.

 protected $hidden = array('password','secret_field'); 

http://laravel.com/docs/eloquent#converting-to-arrays-or-json

+3
Jul 17 '13 at 16:57
source share

Using model:

 Model::where('column','value')->get(['column1','column2','column3',...]); 

Using Query Builder:

 DB::table('table_name')->where('column','value')->get(['column1','column2','column3',...]); 
+1
Oct 05 '17 at 9:35 on
source share

If I understand well, then that returns is fine if you want to see only one column. If so, then this should be much simpler below:

 return Response::eloquent(Theme::with('user')->get(['username'])); 
0
Jan 14 '16 at 10:50
source share

Check out, http://laravel.com/docs/database/eloquent#to-array

You should be able to determine which columns you do not want to display in the api.

-3
Feb 06 '13 at 12:32
source share



All Articles