How to Use DB Raw Query in Laravel?
Hello Folks,
Today, how to use db raw in laravel is our main topic. let’s discuss about laravel raw query example. We will use laravel DB::raw query. This post will give you a simple example of laravel db raw query count.
There is a way to use DB::raw() query in laravel. we can use with the select statement, join query, and where condition. here I will give you four examples as like below:
1. Example 1: DB Raw with Select Clause Query in Laravel
2. Example 2: DB Raw with Count Query in Laravel
3. Example 3: DB Raw with Join Query in Laravel
4. Example 4: DB Raw with Where Condition Query in Laravel
Let's see one by one example:
Example 1: DB Raw with Select Clause Query in Laravel
app/Http/Controllers/DemoController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use DB;
class DemoController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$users = User::select(DB::raw("
name,
email,
(CASE WHEN (type = 1) THEN 'Admin' ELSE 'User' END) as user_type"))
->take(3)
->get();
dd($users);
}
}
Output:
Array
(
[0] => Array
(
[name] => Hardik Savani
[email] => aatmaninfotech@gmail.com
[user_type] => Admin
)
[1] => Array
(
[name] => Vimal Kashiyani
[email] => vimal@gmail.com
[user_type] => User
)
[2] => Array
(
[name] => Deshaun Rowe Sr.
[email] => gstoltenberg@example.org
[user_type] => Admin
)
)
Example 2: DB Raw with Count Query in Laravel
app/Http/Controllers/DemoController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use DB;
class DemoController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$users = User::select(DB::raw('type, count(*) as user_count'))
->groupBy('type')
->get();
dd($users);
}
}
Output:
Array
(
[0] => Array
(
[type] => 1
[user_count] => 3
)
[1] => Array
(
[type] => 0
[user_count] => 42
)
)
Example 3: DB Raw with Join Query in Laravel
app/Http/Controllers/DemoController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Product;
use DB;
class DemoController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$data = Product::select("products.*", "product_stock.quantity_group")
->join(DB::raw("(SELECT
product_stock.id_product,
GROUP_CONCAT(product_stock.quantity) as quantity_group
FROM product_stock
GROUP BY product_stock.id_product
) as product_stock"),function($join){
$join->on("product_stock.id_product","=","products.id");
})
->groupBy("products.id")
->get();
dd($data);
}
}
Example 4: DB Raw with Where Condition Query in Laravel
app/Http/Controllers/DemoController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Order;
use DB;
class DemoController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$data = Order::where('id', DB::raw("(select max(`id`) from orders)"))->get();
dd($data);
}
}
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 Sum Query with Where Condition Example
- How to Search First Name and Last Name in Laravel Query?
- Laravel Group By with Min Value Query Example
- Laravel Group By with Max Value Query Example
- Laravel Search Case Insensitive Query Example
- Laravel Case Sensitive Query Search Example
- PHP Laravel Left Join Query Example
- How to use Union Query with Laravel Eloquent?
- Laravel Groupby Having with DB::raw() Example
- Laravel Eloquent Order By Random Row Example
- Laravel Query Builder Where Exists Example
- How to use DB Transactions in Laravel?
- Example of unionAll in Query Builder Laravel
- Laravel Join with Subquery in Query Builder Example