Home >Database >Mysql Tutorial >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]%'
Matching Results
The code provided in the question:
BookingDates::where('email', Input::get('email')) ->orWhere('name', 'like', Input::get('name')) ->get()
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();
This will generate the correct MySQL query:
SELECT * FROM booking_dates WHERE email='[email protected]' OR name LIKE '%[name]%'
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!