0

Looking for a way to get specific columns from Laravel Eloquent with custom column names.

E.g. get example_name as name.

I am trying to replicate SQL Alias like SELECT column_name AS alias_name but without using custom attributes.

Here is and example query in Laravel:

Table::get(['column1', 'column2', 'column3'])->all()

And what I would like it to get would look something like this:

Table::get(['column1', 'column2 AS column4', 'column3'])->all()
1

1 Answer 1

2

for example in user model email as emailAddress u want to get then in your user model do like that

protected $appends = ['emailAddress']; //add custom column here..

public function getEmailAddressAttribute()
{
     return $this->email;
}

or u can use by query like that

User::select('id','name','email as emailAddress')->get();

Table::select('column1', 'column2 AS column4', 'column3')->get()

for more detail read https://laravel.com/docs/5.8/eloquent-mutators

Sign up to request clarification or add additional context in comments.

2 Comments

I was using 'get' instead of 'select', therefore it was not working for me. Thanks a lot!
@butaminas no worry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.