Home  >  Article  >  Database  >  How Can I Use the `LIKE` Operator with Eloquent in Laravel 5?

How Can I Use the `LIKE` Operator with Eloquent in Laravel 5?

Barbara Streisand
Barbara StreisandOriginal
2024-11-26 07:33:10717browse

How Can I Use the `LIKE` Operator with Eloquent in Laravel 5?

Laravel-5 'LIKE' Equivalent (Eloquent)

When utilizing Eloquent in Laravel 5, the "LIKE" operator can be replicated by using the "orWhereLike" method. However, if this method fails to yield the desired results, it's helpful to comprehend the MySQL statement it triggers.

In the provided code:

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

The corresponding MySQL statement would resemble:

select * from booking_dates where email='[email protected]' or name like Input::get('name');

To accurately mimic the desired query:

select * from booking_dates where email='[email protected]' or name like '%John%'

Employ percent symbols ("%") around the search parameter as demonstrated below:

BookingDates::where('email', Input::get('email'))
    ->orWhere('name', 'like', '%' . Input::get('name') . '%')->get();

The above is the detailed content of How Can I Use the `LIKE` Operator with Eloquent in Laravel 5?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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