Laravel Eloquent Select Column as Alias

I like to use select statements to return specific columns.

Animal::select('size','color')->get();

But I was happy to find out that you can alias your columns just like in sql.

Animal::select('size as units', 'color')->get()

Which can be make things a breeze, depending on what you are doing.

Remember to use the dot syntax to specify a table for joined columns with the same name.

Animal::join('amphibians', 'animals.id', '=', 'amphibians.id')->select('amphibians.color as watercolor')->get()

Leave a Comment