samedi 23 juin 2018

Select specific columns of table and relation in laravel

Lets consider I have a Model User and another Model Employee

Now I want to pluck only some fields of Employee model such as salary, id, emp_id along with some columns of User model such as name,id.

$employee = Employee::with('user:id,name')
             ->where('department', $request->department)
             ->get(['id', 'emp_id', 'salary']);

When I execute this it will return id,emp_id,salary data but for user:name,id it will return null

How can I also specify user:id,name in get() ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire