ItSolutionStuff.com

Laravel Eloquent whereRaw Condition Example

By Hardik Savani β€’ April 16, 2024
Laravel

Now, let's see post of laravel whereraw example. this example will help you laravel eloquent whereraw example. you will learn how to use whereraw in laravel. let’s discuss about whereraw concat in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 project.

Laravel provide new eloquent function whereRaw(), using whereRaw you can easily use mysql function and make it easily where condition. so here i will give you two example one concat function and another date_format function with whereRaw.

You can see bellow syntax on orWhere query in laravel:

whereRaw(SQL Condition, Array Value);

Example 1: Laravel whereRaw() with Concat()

SQL Query:

"select * from `users` where concat(first_name,' ',last_name) = ?"

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()

{

$search = "Hardik Savani";

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

->whereRaw("concat(first_name,' ',last_name) = ?", [$search])

->get();

dd($users);

}

}

Example 2: Laravel whereRaw() with DATE_FORMAT()

SQL Query:

"select * from `users` where DATE_FORMAT(created_at, '%d-%m-%Y') = ?"

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("*")

->whereRaw("DATE_FORMAT(created_at, '%d-%m-%Y') = ?", [date('d-m-Y')])

->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 Where Query Examples

Read Now β†’
β˜…

Laravel Collection Merge | How to Merge Two Eloquent Collection?

Read Now β†’
β˜…

Laravel Multiple Where Condition Example

Read Now β†’
β˜…

Laravel Eloquent WhereNotIn Query Example

Read Now β†’
β˜…

Laravel Eloquent WhereIn Query Example

Read Now β†’
β˜…

How to use Union Query with Laravel Eloquent?

Read Now β†’
β˜…

Laravel Eloquent Relationships Tutorial From Scratch

Read Now β†’
β˜…

Laravel One to Many Eloquent Relationship Tutorial

Read Now β†’
β˜…

Laravel Where Condition with Two Columns Example

Read Now β†’
β˜…

Laravel Where Clause with date_format() Example

Read Now β†’
β˜…

Laravel Query Builder Where Exists Example

Read Now β†’