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


You can make use of db::raw when you want to test or use some arbitrary string inside the fluent query builder.

To work with db::raw you have to make use of the DB facade class −

use Illuminate\Support\Facades\DB;

Here are a few examples that show the use of DB:raw().

Example 1

<?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); } }

Since we want a string to passed to name, we are using the string inside DB::raw as shown below −

update(['name' =>DB::raw("'Heena Khan'")]);

Output

The output of the above code is −

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

Example 2

Another example is using DB::raw() inside DB::update as shown below −

<?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); } }

Output

The output of the above code is as follows −

stdClass Object(
   [id] => 3
   [name] => Seema
   [email] => SZJj8osSc1@gmail.com
   [email_verified_at] =>
   [password] => pDqluHE2x8
   [remember_token] =>
   [created_at] =>
   [updated_at] =>
)

Example 3

We are making use of the DB::statement to execute the query −

<?php namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Support\Str; 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); } }

Output

The output of the above code is −

stdClass Object(
   [id] => 4
   [name] => Neha Singh
   [email] => KBc9tUG3pp@gmail.com
   [email_verified_at] =>
   [password] => jyfM24HRSd
   [remember_token] =>
   [created_at] =>
   [updated_at] =>
)

Example 4

In the example below DB::raw is used inside DB::select and The output of the above code is - displayed as shown below −

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Str; use Illuminate\Support\Facades\DB; class UserController extends Controller{ public function index() { $name = '"Neha Singh"'; $test = DB::select(DB::raw('SELECT * FROM users WHERE id=1')); print_r($test); } }

Output

Array(
   [0] => stdClass Object(
      [id] => 1
      [name] => Siya Khan
      [email] => zt6EovRjV7@gmail.com
      [email_verified_at] =>
      [password] => yhka61wZeS
      [remember_token] =>
      [created_at] =>
      [updated_at] =>
   )
)

Updated on: 30-Aug-2022

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements