Getting specific columns with eager loading laravel 4

前端 未结 2 1665
执念已碎
执念已碎 2020-12-06 03:43

I am new to laravel and making a basic application to get a grip on relationships.I have implemented one to one relationship and want to get specific columns from both the b

2条回答
  •  萌比男神i
    2020-12-06 04:26

    1 You need to always select primary key of the parent and foreign key of the child of a relation, otherwise Eloquent won't be able to match related models.

    2 Closure passed to the eager loading is a value of the array, not 2nd parameter of with function:

    User::with(array('identity_cards' => function($query) {
                $query->select('issuance_location', 'user_id');
          }))->get(array('first_name', 'id'))->toArray();
    

    3 I would rename that relation to identity_card, for it's hasOne - it will be easier to work with.

提交回复
热议问题