Accessor | Can we set using other eloquent model

Posted 10 months ago by sanjay23


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.


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

Reply to

Use Markdown with GitHub-flavored code blocks.