Level 14
I don't see the point of pluck if state is a single model, why not get the value directly $address->state->state_name?
I have three Models named as Lead, Address, State. They are related with one to one relation. like below. On Lead Model:
//On Lead Model:
public function address () {
return $this->belongsTo(Address::clsss, 'account_id');
}
//On Address Model:
public function state() {
return $this->belongsTo(State::clsss, 'state_id');
}
//My Query
$models = Lead::with('address.state')->get();
//sample output:
Array
(
[0] => Array
(
[account_id] => 71526
[lead_address] => Array
(
[account_id] => 75465
[state_id] => 2
[state] => Array (
[state_id] => 2
[state_name] => 'WB'
)
)
)
)
If I map this output like below I get all models from State in state_name key.
$models = $models->map(function ($model) {
$address = $model->address;
$model->state_name = $address->state->pluck('state_name');
return $model;
});
But If I append value in State model with $append property I see n number of duplicate quaries. What am I missing?
Please or to participate in this conversation.