Laravel Query Add Custom Column Example
Hi Guys,
In this example,I will learn you how to select custom column with value in laravel query.you can easy and simply how to select custom column with value in laravel query.
so here, i will give you very simple example for adding custom column with value.
Example:
<?php
namespace App\Http\Controllers;
use App\Models\User;
use DB;
class UserController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$users = User::select([
"id",
"name",
DB::raw("'Admin' as type"),
DB::raw("1 as active")
])->get();
dd($users->toArray());
}
}
Output:
Array
(
[0] => Array
(
[id] => 1
[name] => Keval
[type] => Admin
[active] => 1
)
[1] => Array
(
[id] => 2
[name] => Mehul
[type] => Admin
[active] => 1
)
)
It will help you...
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial