sanjay23
600
10
Eloquent

Accessor | Can we set using other eloquent model

Posted 1 year ago by sanjay23

Hi,

I am using state_id, country_id in my post table, However while fetching post data, I want to show the state name, country name instead. So I would like to know is it possible in model to set accessor in the way it will fetch state name and display in result.

I am trying the way like below

public function state()
    {
        return $this->hasMany('App\Model\State', 'id');
    }

    public function getStateAttribute()
    {
        return $state = $this->state()->find(2307);
    }

It should return state name instead of id. Is it possible, Can anyone suggest solution for this.

Thanks

Please sign in or create an account to participate in this conversation.