Select specific fields in Eloquent eager loading not working Select specific fields in Eloquent eager loading not working laravel laravel

Select specific fields in Eloquent eager loading not working


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


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.


You have to pass foreign_key if you fecth 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]);}