Home > Database > Mysql Tutorial > body text

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

Barbara Streisand
Release: 2024-11-26 07:33:10
Original
715 people have browsed it

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()
Copy after login

The corresponding MySQL statement would resemble:

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

To accurately mimic the desired query:

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

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

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

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!

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