Join WhatsApp ChannelJoin Now

Laravel 8 Add Custom Column Query Example

Hi Dev,

Today, i we will show you laravel 8 add custom column query example. This article will give you simple example of laravel 8 add custom column query example. you will learn laravel 8 add custom column query example. So let’s follow few step to create example of laravel 8 add custom column query 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] => codeplaners
            [type] => Admin
            [active] => 1
        )
    [1] => Array
        (
            [id] => 2
            [name] => rakesh
            [type] => Admin
            [active] => 1
        )
)

Recommended Posts