Laravel Search Case Insensitive Query Example

By Hardik Savani April 16, 2024 Category : Laravel

In this tutorial, you will learn laravel case insensitive query. it's simple example of laravel where case insensitive query. you will learn laravel case insensitive search query. if you want to see example of laravel case insensitive query with where condition then you are a right place. You just need to some step to done how to make case insensitive query in laravel query builder.

Sometime we have records in users table with following name like "Hardik", "HARDIK" and "hardik". now you just need to get case insensitive name like if you search "Hardik" then it should get all records even if it's in uppercase, lowercase or capitalize. so that solution i will give you simple example and show you how to make case insensitive query using mysql lower in laravel. you can use this example in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.

Let's see solution and check full example as bellow:

Solution:

$users = User::select("id", "name", "email")

->where(DB::raw('lower(name)'), strtolower("Hardik"))

->get();

Example

users table:

Controller Code:

<?php

namespace App\Http\Controllers;

use App\Models\User;

use DB;

class ITSController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$users = User::select("id", "name", "email")

->where(DB::raw('lower(name)'), strtolower("Hardik"))

->get()

->toArray();

dd($users);

}

}

Output:

Array

(

[0] => Array

(

[id] => 1

[name] => Hardik

[email] => carri13@nhpc.us

)

[1] => Array

(

[id] => 3

[name] => HARDIK

[email] => aatmaninfotech1sdsd@gmail.co

)

[2] => Array

(

[id] => 4

[name] => hardik

[email] => fanny.baumbach@example.org

)

)

I hope it can help you...

Tags :
Shares