Laravel-5 'LIKE' equivalent (Eloquent)

PhpMysqlLaravel 5

Php Problem Overview


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

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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

Solution 3 - Php

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

Solution 4 - Php

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.

Solution 5 - Php

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

Solution 6 - Php

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

Solution 7 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionV4n1ll4View Question on Stackoverflow
Solution 1 - PhpPawel BieszczadView Answer on Stackoverflow
Solution 2 - PhpOlegView Answer on Stackoverflow
Solution 3 - Phpsadiq rashidView Answer on Stackoverflow
Solution 4 - PhpJaredDmzView Answer on Stackoverflow
Solution 5 - PhpMamoonView Answer on Stackoverflow
Solution 6 - PhpmanzedeView Answer on Stackoverflow
Solution 7 - PhpAnkush_SharmaView Answer on Stackoverflow