How to update a query on Laravel Fluent using db::raw?

You can use DB::raw() when you want to execute arbitrary SQL strings inside Laravel's Fluent query builder. This is particularly useful for complex SQL expressions or when you need to bypass Laravel's query builder methods.

Note: To work with DB::raw(), you need to include the DB facade class:
use Illuminate\Support\Facades\DB;

Using DB::raw() with Update Query

You can use DB::raw() within the update() method to pass raw SQL values

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserController extends Controller{
   public function index() {
      DB::table('users')
      ->where('id', 2)
      ->update(['name' => DB::raw("'Heena Khan'")]);
      
      $user = DB::table('users')->find(2);
      print_r($user);
   }
}
?>

The output shows the updated record

stdClass Object(
   [id] => 2
   [name] => Heena Khan
   [email] => J17NQEuksw@gmail.com
   [email_verified_at] =>
   [password] => hkGekGafje
   [remember_token] =>
   [created_at] =>
   [updated_at] =>
)

Using DB::raw() with Raw SQL Update

You can execute complete raw SQL statements using DB::update() with DB::raw()

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserController extends Controller{
   public function index() {
      $name = '"Seema"';
      $test = DB::update(DB::raw('UPDATE users SET name='.$name.' WHERE id =3'));
      
      $user = DB::table('users')->find(3);
      print_r($user);
   }
}
?>
stdClass Object(
   [id] => 3
   [name] => Seema
   [email] => SZJj8osSc1@gmail.com
   [email_verified_at] =>
   [password] => pDqluHE2x8
   [remember_token] =>
   [created_at] =>
   [updated_at] =>
)

Using DB::raw() with DB::statement()

For executing raw SQL statements, you can use DB::statement() with DB::raw()

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserController extends Controller{
   public function index() {
      $name = '"Neha Singh"';
      $test = DB::statement(DB::raw('UPDATE users SET name='.$name.' WHERE id =4'));
      
      $user = DB::table('users')->find(4);
      print_r($user);
   }
}
?>
stdClass Object(
   [id] => 4
   [name] => Neha Singh
   [email] => KBc9tUG3pp@gmail.com
   [email_verified_at] =>
   [password] => jyfM24HRSd
   [remember_token] =>
   [created_at] =>
   [updated_at] =>
)

Using DB::raw() with Select Queries

You can also use DB::raw() with select statements for complex queries

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserController extends Controller{
   public function index() {
      $test = DB::select(DB::raw('SELECT * FROM users WHERE id=1'));
      print_r($test);
   }
}
?>
Array(
   [0] => stdClass Object(
      [id] => 1
      [name] => Siya Khan
      [email] => zt6EovRjV7@gmail.com
      [email_verified_at] =>
      [password] => yhka61wZeS
      [remember_token] =>
      [created_at] =>
      [updated_at] =>
   )
)

Conclusion

DB::raw() is powerful for executing complex SQL expressions in Laravel. Always sanitize user input when using raw queries to prevent SQL injection attacks.

Updated on: 2026-03-15T10:09:36+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements