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
- How to prevent host header attack in Laravel
- On delete set foreign id column value null using migration in laravel 8
- Count all and get 10 records after where condition in laravel
- How to check record exist or not in relationship table
- How to create pivot table in laravel using migration
- Import/Use Storage facade in laravel
- Create record with unique slug in laravel
- JQuery each loop on json response after ajax in laravel
- Insert dummy data in users table Laravel
- Get last week data in Laravel
- Laravel create default admin user
- Delete file from amazon s3 bucket using Laravel
- Method chaining in Laravel
- Get products with number of orders in Laravel
- Php artisan make model, factory, migration and controller in single command
- How to send email in Laravel 11
- PhpMyAdmin - Error The mysqli extension is missing
- How to get specific columns using with method in laravel Eloquent relationship
- Eager loading dynamically in laravel
- Wheredate in laravel not working
- How to Get records between two dates in Laravel
- Laravel create multiple records in Pivot table
- Post table seeder laravel 10
- Class 'App\Providers\Auth' not found
- How to add class to tr in table using foreach in laravel