Getting specific columns with eager loading laravel 4

前端 未结 2 1662
执念已碎
执念已碎 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条回答
  • 2020-12-06 04:14

    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');
        }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题