Home > Backend Development > PHP Tutorial > How to Perform Case-Insensitive Searches with LIKE in Laravel 5\'s Eloquent ORM?

How to Perform Case-Insensitive Searches with LIKE in Laravel 5\'s Eloquent ORM?

Linda Hamilton
Release: 2024-10-25 07:13:28
Original
302 people have browsed it

How to Perform Case-Insensitive Searches with LIKE in Laravel 5's Eloquent ORM?

Laravel-5 'LIKE' Equivalent (Eloquent)

When searching for database records using Laravel 5's Eloquent ORM, the where method can be used with the LIKE operator to perform case-insensitive comparisons. However, the syntax for this operator in Laravel differs from the standard SQL LIKE syntax.

Question:

A developer is attempting to execute the following query using Laravel 5's Eloquent ORM:

BookingDates::where('email', Input::get('email'))->orWhere('name', 'like', Input::get('name'))->get()
Copy after login

However, no results are being returned. The developer is unsure why the orWhereLike clause is not matching any results.

Answer:

The orWhereLike syntax accepts two arguments: the column name to be searched and the search term. In this case, the name column is being searched for a value that contains the user input specified by Input::get('name').

However, the code provided uses the orWhere method, not the orWhereLike method. To perform a case-insensitive search using LIKE, the correct syntax is:

orWhere('name', 'like', '%' . Input::get('name') . '%')
Copy after login

This code will produce the following MySQL statement:

SELECT * FROM booking_dates WHERE email='[email protected]' OR name LIKE '%John%'
Copy after login

By using the percentage signs (%) in the search term, the query will match any records where the name column contains the specified string, regardless of its position within the column value.

The above is the detailed content of How to Perform Case-Insensitive Searches with LIKE in Laravel 5's Eloquent ORM?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template