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(["*"]    
}]);
Birendra Gurung
  • 2,168
  • 2
  • 16
  • 29
  • 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. – online Thomas May 31 '18 at 11:22
  • Is there a specific reason you are using $notifications instead of Notification model? – Pol Lluis May 31 '18 at 11:23
  • 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 https://laravel.com/docs/5.6/eloquent-collections#available-methods – Pol Lluis May 31 '18 at 11:27
  • @GaimZz This is not true: https://laravel.com/docs/5.6/eloquent-relationships#lazy-eager-loading – online Thomas May 31 '18 at 11:31
  • mmm I see thanks :D – Pol Lluis May 31 '18 at 11:32
  • 2
    Eager loading only works if you select the foreign key column (`id`). – Jonas Staudenmeir May 31 '18 at 12:10
  • @GaimZz the notification model is injected through the service container. BTW does it make any difference in eager loading? – Birendra Gurung May 31 '18 at 17:53
  • @GaimZz Eager loading does works but the select part is the main issue here. – Birendra Gurung May 31 '18 at 17:54
  • @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 not – Pol Lluis May 31 '18 at 17:56
  • @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. – Birendra Gurung May 31 '18 at 18:08

3 Answers3

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

Pol Lluis
  • 1,152
  • 7
  • 17
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.

Birendra Gurung
  • 2,168
  • 2
  • 16
  • 29
  • 4
    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. – Jannie Theunissen Aug 11 '18 at 17:24
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]);
}
Macdonald
  • 880
  • 7
  • 21
Mahedi Hasan Durjoy
  • 1,031
  • 13
  • 17