Laravel Eloquent selectRaw() Query Example
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
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Eloquent orderByRaw() Query Example
- Multiple orWhere Condition in Laravel Eloquent
- Laravel Eloquent Where Query Examples
- Laravel Collection Merge | How to Merge Two Eloquent Collection?
- How to Create and Use Query Scope in Laravel Eloquent
- Laravel Eloquent Concat Two Columns Example
- How to use Union Query with Laravel Eloquent?
- Laravel Select with Count Query with Group By Example
- Laravel Select with Sum Query Example
- How to Concat Two Columns in Laravel?
- Laravel Eloquent Where Like Query Example Tutorial