
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": "[email protected]" } 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
- SQLSTATE[42000]: Syntax error or access violation: 1055
- Attempt to read property "avatar" on null in Laravel
- How to avoid duplicate entries in pivot table in Laravel
- Laravel order by date not working
- How to get IP address in laravel
- Call to undefined method Illuminate\Support\Facades\Request::all()
- Class 'App\Providers\Auth' not found
- Call to undefined relationship [user] on model [App\Models\Post]
- How to add a key value pair to existing array in laravel
- How to get selected categories on edit record with Select2
- How to show data by ID in laravel?
- How to add unique records in pivot columns of Laravel pivot table
- The openssl extension is required for SSL/TLS protection but is not available
- Add a subselect based on relationship using withAggregate method
- How to include header file in laravel
- Delete file from amazon s3 bucket using Laravel
- RuntimeException You must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
- In order to use the Auth::routes() method, please install the laravel/ui package
- How to restore multiple records after soft-deletes in Laravel
- SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
- Call to undefined method App\Models\User::follow()
- How to display 1 day ago in comments in laravel view
- How to add foreign key in laravel using migration
- Create project table with model and migration
- How to insert dynamic value to additional column in pivot table in laravel