
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
- Retain selected value of select box in Laravel
- How to send email in laravel
- Create records using relationship in laravel
- Print query in laravel
- Redirect to another view from controller in laravel
- How to avoid duplicate entries in pivot table in Laravel
- How to run a specific seeder class in laravel
- Drop foreign key column in Laravel using migration
- How to check record exist or not in relationship table
- How to make Copy or Duplicate table row in laravel
- Laravel csrf token mismatch for ajax POST Request
- How to check column value of a record is null or not in laravel
- First and last item of the array using foreach iteration in laravel blade
- How to add a key value pair to existing array in laravel
- RuntimeException You must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
- Class 'App\Rules\Hash' not found in Laravel
- Retrieve count of nested relationship data in Laravel
- SQLSTATE[42000]: Syntax error or access violation: 1055
- The POST method is not supported for this route. Supported methods: PUT.
- Get the post details if it has at least one comment in comments table
- Get current month records in laravel 7/8
- Permanently delete a record in laravel
- Conditional where clause in Laravel
- How to get user information using hootlex/laravel-friendships package in laravel
- JQuery each loop on json response after ajax in laravel