Site Admin
Site Admin Founder of MeaningArticles
1381 Views

Laravel Query Add Custom Column

Hello Dev.

In this example, i will learn you the way to pick out custom column with value in laravel query. you could easy and simply how to pick out 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] => Tom
            [type] => Admin
            [active] => 1
        )
    [1] => Array
        (
            [id] => 2
            [name] => Jon
            [type] => Admin
            [active] => 1
        )
)

i'm hoping it assist you to, thanks for visit my article if you like my article then proportion together with your friend and social platform.