我的功能可以采用字符串,并且对其进行了(非常宽的)与多个字段(aka。几乎所有我的DB字段)匹配。桌子)。这似乎有些kludgy,但起作用。但是,这不是我目前的主要关注点。
是否可以:显示哪个"或者"将记录返回到集合中?我想(在结果视图上)显示字符串匹配的记录的哪一部分。
$apps = Application::all();
$apps->where('bill_company_name', 'like', '%'.$request->filter.'%');
$apps->orwhere('bill_address', 'like', '%'.$request->filter.'%');
$apps->orwhere('bill_city', 'like', '%'.$request->filter.'%');
...
$apps = $apps->paginate();
$apps->withPath('custom/url');
return $apps;
我知道我可能可以在视图上执行此操作(通过更多的代码将过滤器再次与记录相加),但是此选项听起来更加努力。
谢谢!
您可以做:
$records = Model::query();
$records = $records->where(‘field’, ‘value’);
…
$records_where = $records->orWhere(‘field’, ‘value’)->get();
if($records_where->isNotEmpty()){
// save into array this one that matched
}
$records = Model::query();
,然后在匹配的视图上查看迭代,但是如果您的字段太多,此过程可能会有些安静……
在您的模型中,
public function getMatch($str) {
if (strpos($this->bill_address, $str) !== false) {
return "bill_address";
} elseif (...) { ... }
}
并没有真正回答这个问题,因此可能有点偏离主题,但可能会在将来对您有所帮助:正如您已经注意到您所做的事情有点混乱,我建议通过不同的技术解决问题。例如,Lucene搜索引擎(Solr和Elastic-search使用)提供了一种调试功能,可用于详细说明如何组成返回记录的分数,以便您可以看到查询命中的哪一部分。
和更快的速度:)https://lucene.apache.org/solr/guide/6_6/common-query-parameters.html#commonquommonquommonquommonquormonquomparameters-thedebugparameter
您可以尝试吗order--> or Where
https://laravel.com/docs/5.5/queries#where-clauses