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
One of the solution would be just making a new relation similar to that you have made, but tweaking it a bit, for conviniece :
public function identity_cards_limited()
{
return $this->hasOne('IdentityCard')->select('desired_column');
}
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.