Laravel 8 Run Case Sensitive Query Tutorial

Reading Time: 4 minutes
6,389 Views

Inside this article we will see how to execute case sensitive query in laravel 8. Case sensitive simply means the keyword should match exact with the pattern.

Laravel 8 run case sensitive query is very interesting tutorial to see and super easy to implement. If query doesn’t have any idea about matching exact pattern i.e case sensitivity then it normally returns the data set when it matches only with no case.

Example – India, india, INDIA will be same if no case sensitive matches. But India is not equals to india if we need case sensitive too.

Case Sensitive Means,

Case Insensitive Means,

To add the concept of case sensitive query pattern in laravel 8 we will use the concept of mysql Binary.

Learn More –

Let’s get started.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

composer create-project --prefer-dist laravel/laravel blog

To start the development server of Laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.


Laravel 8 Case Insensitive Query

Suppose, we have a database table called “countries”. Inside this table we have countries rows.

Let’s take an example row from that.

Query to select Afghanistan row from table –

$country_info = Country::select("id", "sortname", "phonecode")
                        ->where("name", "Afghanistan")
                        ->get()
                        ->toArray();

OR

$country_info = Country::select("id", "sortname", "phonecode")
                        ->where("name", "afghanistan")
                        ->get()
                        ->toArray();

Inside this query where no case matching –

where("name", "Afghanistan") Equals To where("name", "afghanistan")

It returns the same output –


Laravel 8 Case Sensitive Query

In this case, we will add the concept of mysql binary which matches the exact keyword with case sensitive too.

$country_info = Country::select("id", "sortname", "phonecode")
                        ->where(Country::raw("BINARY name"), "Afghanistan")
                        ->get()
                        ->toArray();

We have used mysql binary here – Country::raw(“BINARY name”). When we run it returns

Here,

Afghanistan Not Equals To afghanistan

If we change query a bit like this –

 ->where(Country::raw("BINARY name"), "afghanistan")

It returns –

We hope this article helped you to learn Laravel 8 Run Case Sensitive Query Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.