233

I'm using the below code to pull some results from the database with Laravel 5.

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

However, the orWhereLike doesn't seem to be matching any results. What does that code produce in terms of MySQL statements?

I'm trying to achieve something like the following:

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

7 Answers 7

518

If you want to see what is run in the database use dd(DB::getQueryLog()) to see what queries were run.

Try this

BookingDates::where('email', Input::get('email')) ->orWhere('name', 'like', '%' . Input::get('name') . '%')->get(); 
Sign up to request clarification or add additional context in comments.

5 Comments

is this query sql injection protected?
@partho Yes. Laravel screens the whole string that you pass as the third argument of the where method.
While injection protected you might want to check for unexpected % in user input. E.g., LIKE "%John%" and LIKE "John%" perform differently (you might only intend the latter). Also consider empty input, and then of "%" alone, which also might lead to unintended results from the above code.
Agreed with Ian. Laravel only does partial escaping. There's still a lot of mischief possible if you don't properly escape the LIKE. Here's how: stackoverflow.com/a/42028380/329062
I added preg_replace("/[^A-Za-z0-9 ]/", '', $search); because I don't need special chars, and it protects it from % injection too
35

I have scopes for this, hope it help somebody. https://laravel.com/docs/master/eloquent#local-scopes

public function scopeWhereLike($query, $column, $value) { return $query->where($column, 'like', '%'.$value.'%'); } public function scopeOrWhereLike($query, $column, $value) { return $query->orWhere($column, 'like', '%'.$value.'%'); } 

Usage:

$result = BookingDates::whereLike('email', $email)->orWhereLike('name', $name)->get(); 

Comments

20
$data = DB::table('borrowers') ->join('loans', 'borrowers.id', '=', 'loans.borrower_id') ->select('borrowers.*', 'loans.*') ->where('loan_officers', 'like', '%' . $officerId . '%') ->where('loans.maturity_date', '<', date("Y-m-d")) ->get(); 

1 Comment

->where('loan_officers', 'like', '%' . $officerId . '%') where loan_officers is the serialized field
19

I think this is better, following the good practices of passing parameters to the query:

BookingDates::whereRaw('email = ? or name like ?', [$request->email,"%{$request->name}%"])->get(); 

Better:

BookingDates::where('email',$request->email) ->orWhere('name','like',"%{$request->name}%")->get(); 

You can see it in the documentation, Laravel 5.5.

You can also use the Laravel scout and make it easier with search. Here is the documentation.

2 Comments

Raw is never better. You should sacrifice functionality if it requires raw to achieve it, or approach it differently.
Be aware of sql injection vulnerabilities when using raw statements, as the document says Raw statements will be injected into the query as strings, so you should be extremely careful to avoid creating SQL injection vulnerabilities.
11

the query which is mentioned below worked for me maybe it will be helpful for someone.

 $platform = DB::table('idgbPlatforms')->where('name', 'LIKE',"%{$requestedplatform}%")->first(); 

Comments

-2

If you wish to use it on controller you can do something like:

$generatequery = 'select * from blogs where is_active = 1 and blog_name like '%'.$blogs.'%' order by updated_at desc, id desc';

$blogslists = DB::select($generatequery);

Comments

-3

If you are using Postgres, The key word ILIKE can be used instead of LIKE to make the match case-insensitive according to the active locale. This is not in the SQL standard but is a PostgreSQL extension.

this worked for me.

User::where('name, 'ILIKE', $search)->get(); 

postgres documentation

4 Comments

I do not get the connection between What does that code produce in terms of [SQL] statements? and above reply - what question does this answer?
then using this isn't safe
LIKE is case-insensitive, what collation are you using for it to not be insensitive, definitely not utf8mb4...
@zanderwar WRONG !!! - in Postgresql LIKE is case-sensitive and you have to use ILIKE to make it case-insensitive. The key word ILIKE can be used instead of LIKE to make the match case-insensitive according to the active locale. This is not in the SQL standard but is a PostgreSQL extension. docs

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.