ItSolutionStuff.com

Laravel Eloquent selectRaw() Query Example

By Hardik Savani • April 16, 2024
Laravel

Hello all! In this article, we will talk about laravel selectraw example. you'll learn selectraw query in laravel example. it's simple example of laravel add select raw. I’m going to show you about laravel selectraw field. you will do the following things for laravel eloquent selectraw.

In this example i will give you very simple example of how to use selectRaw in laravel application. you can easily use it with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application.

So, let's see bellow examples that will help you how to use selectRaw() eloquent query in laravel.

Example 1:

SQL Query:

select *, amount + ? as amount_with_bonus

from `users`

Laravel Query:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$users = User::select("*")

->selectRaw('amount + ? as amount_with_bonus', [500])

->get();

dd($users);

}

}

Output:

Array

(

[0] => Array

(

[id] => 1

[name] => Prof. Josiane Jast MD

[email] => savanihd@gmail.com

[email_verified_at] => 2020-07-01T09:34:13.000000Z

[created_at] => 2020-07-03T09:34:13.000000Z

[updated_at] => 2020-07-01T09:34:13.000000Z

[status] => 1

[first_name] =>

[last_name] =>

[point] =>

[points] => 0

[amount] => 1000

[amount_with_bonus] => 1500

)

[1] => Array

(

[id] => 2

[name] => Mr. Donavon Beer

[email] => yemmerich@example.net

[email_verified_at] => 2020-07-01T09:34:13.000000Z

[created_at] => 2020-07-02T09:34:13.000000Z

[updated_at] => 2020-07-01T09:34:13.000000Z

[status] => 1

[first_name] => Hardik

[last_name] => Savani

[point] =>

[points] => 0

[amount] => 1000

[amount_with_bonus] => 1500

)

)

Example 2:

Laravel Query:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

use DB;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$users = User::select("*")

->select('*', DB::raw('amount + 500 as amount_with_bonus'))

->get();

dd($users);

}

}

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 orderByRaw() Query Example

Read Now →
ā˜…

Multiple orWhere Condition in Laravel Eloquent

Read Now →
ā˜…

Laravel Eloquent Where Query Examples

Read Now →
ā˜…

Laravel Collection Merge | How to Merge Two Eloquent Collection?

Read Now →
ā˜…

How to Create and Use Query Scope in Laravel Eloquent

Read Now →
ā˜…

Laravel Eloquent Concat Two Columns Example

Read Now →
ā˜…

How to use Union Query with Laravel Eloquent?

Read Now →
ā˜…

Laravel Select with Count Query with Group By Example

Read Now →
ā˜…

Laravel Select with Sum Query Example

Read Now →
ā˜…

How to Concat Two Columns in Laravel?

Read Now →
ā˜…

Laravel Eloquent Where Like Query Example Tutorial

Read Now →