php Laravel-5 'LIKE' 等价物 (Eloquent)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30761950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel-5 'LIKE' equivalent (Eloquent)
提问by V4n1ll4
I'm using the below code to pull some results from the database with Laravel 5.
我正在使用下面的代码从 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?
但是, orWhereLike 似乎没有匹配任何结果。该代码在 MySQL 语句方面产生什么?
I'm trying to achieve something like the following:
我正在尝试实现以下目标:
select * from booking_dates where email='[email protected]' or name like '%John%'
回答by Pawel Bieszczad
If you want to see what is run in the database use dd(DB::getQueryLog())
to see what queries were run.
如果您想查看数据库dd(DB::getQueryLog())
中运行的内容,请使用查看运行的查询。
Try this
尝试这个
BookingDates::where('email', Input::get('email'))
->orWhere('name', 'like', '%' . Input::get('name') . '%')->get();
回答by sadiq rashid
$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();
回答by Oleg
I have scopes for this, hope it help somebody.
我有这个范围,希望它可以帮助某人。
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();
回答by JaredDmz
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();
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.
您还可以使用 Laravel scout 并简化搜索。 这是文档。