ItSolutionStuff.com

How to Select Custom Column with Value in Laravel Query?

By Hardik Savani • November 5, 2023
Laravel

Hi,

This tutorial will provide example of laravel query add custom column. you'll learn add custom column in laravel query. I’m going to show you about add a new column with a value to the select query in laravel. I’m going to show you about laravel query static value with column name. follow bellow step for how to select custom column values in laravel query.

Sometime, we need to add column with static value like tax and any for calculation on your laravel eloquent query. so here, i will give you very simple example for adding custom column with value.

let's see bellow example:

Example:

<?php

namespace App\Http\Controllers;

use App\Models\Product;

use DB;

class ITSController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$products = Product::select([

"id",

"name",

DB::raw("'10%' as tax"),

DB::raw("1 as active")

])->get();

dd($products->toArray());

}

}

Output:

Array

(

[0] => Array

(

[id] => 1

[name] => Samsung Galaxy

[tax] => 10%

[active] => 1

)

[1] => Array

(

[id] => 2

[name] => Apple iPhone 12

[tax] => 10%

[active] => 1

)

[2] => Array

(

[id] => 3

[name] => Google Pixel 2 XL

[tax] => 10%

[active] => 1

)

[3] => Array

(

[id] => 4

[name] => LG V10 H800

[tax] => 10%

[active] => 1

)

)

i hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

šŸ“ŗ Subscribe on YouTube

We Are Recommending You

ā˜…

Laravel Eloquent Order By Query Example

Read Now →
ā˜…

Laravel Eloquent orWhere() Condition Example

Read Now →
ā˜…

Laravel Eloquent WhereIn Query Example

Read Now →
ā˜…

How to display query log in Laravel 7/6?

Read Now →
ā˜…

Laravel Eloquent Group By with Multiple Columns Example

Read Now →
ā˜…

Laravel Select with Sum Query Example

Read Now →
ā˜…

How to Get Query Strings Value in Laravel?

Read Now →
ā˜…

Laravel Query Builder Where Exists Example

Read Now →
ā˜…

Example of unionAll in Query Builder Laravel

Read Now →
ā˜…

Laravel Join with Subquery in Query Builder Example

Read Now →
ā˜…

How to Get Query Log in Laravel Eloquent?

Read Now →