Home > Database > Mysql Tutorial > body text

Laravel 5 Query Builder: How to Correctly Use `orWhereLike()` with Wildcards?

Susan Sarandon
Release: 2024-11-28 06:07:10
Original
699 people have browsed it

Laravel 5 Query Builder: How to Correctly Use `orWhereLike()` with Wildcards?

Laravel 5 Query Builder: 'LIKE' Syntax Equivalent

In Laravel 5, the LIKE operator in Eloquent queries is represented by the 'whereLike()' method. However, some users have encountered difficulties in using the 'orWhereLike()' method to combine LIKE clauses.

MySQL Equivalent

The 'orWhereLike()' method will generate a MySQL query in the following format:

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

Matching Results

The code provided in the question:

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

Will not match any results because the 'name' condition is missing the wildcard characters, which are required to indicate a partial match.

Correct Syntax

To achieve the intended MySQL query, use the following code:

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

This will generate the correct MySQL query:

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

The above is the detailed content of Laravel 5 Query Builder: How to Correctly Use `orWhereLike()` with Wildcards?. 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