Cully Mason
Cully Mason

Reputation: 463

Is there a way to create an alias for a column name every time it is called in Laravel 4?

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

Answers (1)

diegofelix
diegofelix

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

Related Questions