Reputation: 463
I have a model course who has a mentor_id column. Is there a way to call Course::find($id);
and return with the column alias mentor
instead of mentor_id
? Is there a way to do it without having to select which columns are returned? It is a rather large table.
Upvotes: 0
Views: 193
Reputation: 396
Yes.
You can create a Mutator to "fake" a field and instead return a real field.
example:
public function getMentorAttribute()
{
return $this->mentor_id;
}
Now, you can call:
Course::find($id)->mentor;
Check the Mutators Documentation
Upvotes: 1