How to return a column with different name in Laravel
How to return a column with different name in Laravel
In this session, we will try our hand at solving the "How to return a column with different name in Laravel".
Sometimes you have to get the column of the table with a different name. In that case you can use as in the select to get the column with a different column name.-
Return a column with different name in Laravel using Eloquent Query Builder
$users = DB::table('users')->select('name', 'email as user_email')->get(); dd($users);
Output:
^ Illuminate\Support\Collection {#1437 ▼ #items: array:15 [▼ 0 => {#1438 ▼ +"name": "summer" +"user_email": "john@example.com" } 1 => {#1441 ▶} 2 => {#1442 ▶} 3 => {#1443 ▶} 4 => {#1444 ▶} 5 => {#1445 ▶} 6 => {#1446 ▶} 7 => {#1447 ▶} 8 => {#1448 ▶} 9 => {#1449 ▶} 10 => {#1450 ▶} 11 => {#1451 ▶} 12 => {#1452 ▶} 13 => {#1453 ▶} 14 => {#1454 ▶} ] }
It will return the records from the users table with column email as user_email. Output may varies. In may case i have 15 records in my users table which return name and email as user_email after using this code snippet.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Command to create MySQL Docker image and access the MySQL command-line interface (CLI) within a running Docker container
- Laravel pagination links with query string
- Add a subselect based on relationship using withAggregate method
- If condition in Laravel 9
- Composer create project laravel/laravel example app
- Insert values in pivot table dynamically in laravel
- Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, null given
- Rendering HTML from database table to view in Laravel
- Laravel create multiple records in Pivot table
- How to insert value to additional columns in pivot table in laravel
- In order to use the Auth::routes() method, please install the laravel/ui package
- How to customize or Change validation error messages
- Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused)"
- Get 30 days older records from table in laravel
- Conditional where clause in Laravel
- Create records using relationship in laravel
- File_put_contents(/var/www/html/w3code/storage/framework/sessions/CXwN3EXKxERD6jgy3rADcaAAbAx8FRKih2JK7UO9): Failed to open stream: Permission denied
- Calculate age from date of birth in Laravel
- Get ids in array from users table
- How to create laravel project using composer
- Return view from route Laravel
- How to create project_user pivot table in laravel
- Remove array keys and values if it does not exist in other array in Laravel
- How to pass data to partial view file in laravel
- Convert input array to comma-separated string in laravel controller