Laravel Eloquent Select Column as Alias


If you’re like me and like to use select statements to return specific columns. You’ll be happy to know that Eloquent allow you to use select column as alias just like in SQL.

Animal::select('size as units')->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', '', '=', '')->select('amphibians.color as watercolor')->get()

Leave a Comment