Reputation: 670
How to select certain columns on parents table while querying with eager loading?
$accounts = \App\Account::with('status')->get();
Return
[{"id":1,"username":"kuhn.desiree","email":"[email protected]","last_login":"2009-04-02 23:21:20","created_at":"2017-07-15 19:07:03","updated_at":"2017-07-15 19:07:03","status_id":13,"status":{"id":13,"name":"ab","desc":"Quos quas.","created_at":"2017-07-15 19:07:01","updated_at":"2017-07-15 19:07:01"}}]
I only want username
and email
on Account
table.
Upvotes: 1
Views: 1095
Reputation: 4173
one way is using select()
method
$accounts = \App\Account::with('status')->select('username', 'email','status_id')->get();
If you would like to retrieve a Collection containing the values of a single column, you may use the pluck method.:
accounts = \App\Account::with('status')->pluck('username', 'email');
Upvotes: 0
Reputation: 4205
Try to use this code:
$accounts = \App\Account::with(['status' => function($q)
{
$q->select('username', 'email');
}])->get();
Upvotes: 1
Reputation: 1356
You want to add a select call:
$accounts = \App\Account::with('status')->select('username', 'email')->get();
Upvotes: 0