V4n1ll4
V4n1ll4

Reputation: 6099

Laravel-5 'LIKE' equivalent (Eloquent)

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%'

Upvotes: 229

Views: 635105

Answers (7)

JaredDmz
JaredDmz

Reputation: 183

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.

Upvotes: 18

Ankush_Sharma
Ankush_Sharma

Reputation: 1

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);

Upvotes: -1

manzede
manzede

Reputation: 47

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

Upvotes: -3

M Mamoon Khan
M Mamoon Khan

Reputation: 179

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();

Upvotes: 10

Oleh Diachenko
Oleh Diachenko

Reputation: 662

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();

Upvotes: 35

sadiq rashid
sadiq rashid

Reputation: 546

$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();

Upvotes: 20

Pawel Bieszczad
Pawel Bieszczad

Reputation: 13325

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();

Upvotes: 514

Related Questions