How to change database column name in Eloquent query results

Updated on 26-Nov-2021 in Questions
291

Often times, developers are required to output query results with a specific column name (other than what is in the database table). In order to set specific name of the column column in the results use “as” in the eloquent query.

Example code:

$users = DB::table('users')
->select('name', 'email as username')
->get();

In the above example, result will show column name as username instead of email.

Published by:

Laravel Fan

1 rated the article helpful.

Was this article helpful for you?

Yes No