Select specific fields in Eloquent eager loading not working
Asked Answered
T

3

8

I have a Notifications model that belongs to a User. I want to load the user when a collection of notifications is selected and only load the model with names and emails. However, when using the select method, the query returns null.

$notifications->load(['user' => function($query){
    $query->select(["name","email"]);
}]);

If the parameter for select() method is given as below, it gives all the fields:

$notifications->load(['user' => function($query){
    $query->select(["*"]    
}]);
Thithia answered 31/5, 2018 at 11:16 Comment(10)
Just a question: why would you select when eager loading? If you want to filter results for the response you can add protected $hidden = [] to your model.Burk
Is there a specific reason you are using $notifications instead of Notification model?Bruton
if you have a collection already created you can't cast ->load on that collection, atleast load method doesn't appear on collection avaiable methods of laravel docs laravel.com/docs/5.6/eloquent-collections#available-methodsBruton
@GaimZz This is not true: laravel.com/docs/5.6/eloquent-relationships#lazy-eager-loadingBurk
mmm I see thanks :DBruton
Eager loading only works if you select the foreign key column (id).Sanders
@GaimZz the notification model is injected through the service container. BTW does it make any difference in eager loading?Thithia
@GaimZz Eager loading does works but the select part is the main issue here.Thithia
@BirendraGurung, if my answer isn't working you can try doing your select like this-> $query->select(["id","name","email"]), because as the laravel docs state, when using this you must always select id whether you want or notBruton
@JonasStaudenmeir Yes, adding primary key to the parameter was the solution. All I did was added the primary key column (in my case: ID) and that worked in my case.Thithia
T
9

Just needed to add the primary key column and that worked fine.

$notifications->load(['user' => function($query){
    $query->select(["ID","name","email"]);
}]);

This way any constraints can be added to the query.

Thithia answered 31/5, 2018 at 18:18 Comment(1)
If you have it the other way around you need to include the foreign key: $user->load(['notifications' => function($q){ $q->select(['user_id', 'message']); }]); because 'id' won't work.Grosgrain
B
10

Maybe selecting the fields like this works out:

$notifications->load('user:id,name,email');

What you are trying is designed to add constraints, not to select some fields

Bruton answered 31/5, 2018 at 11:37 Comment(1)
it won't work with closures when you need to add some constraints to your query.Thithia
T
9

Just needed to add the primary key column and that worked fine.

$notifications->load(['user' => function($query){
    $query->select(["ID","name","email"]);
}]);

This way any constraints can be added to the query.

Thithia answered 31/5, 2018 at 18:18 Comment(1)
If you have it the other way around you need to include the foreign key: $user->load(['notifications' => function($q){ $q->select(['user_id', 'message']); }]); because 'id' won't work.Grosgrain
H
0

You have to pass the foreign_key if you fetch data with select. Have a look

public function index()
{ 
    $employee = Employee::with(['pay_salary' => function($query){
       $query->select(['employee_id','salary_amount'])
             ->whereMonth('paying_date',\Carbon\Carbon::now()->month);
    },'getSalary:id,employeeID,salary'])->get();
    
    return View::make('admin.salary.index',['data' => $employee]);
}
Haematoid answered 11/10, 2020 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.