使用多个'orwhere'子句搜索模型

I have a function that takes a string and does a (very broad) match for it against multiple fields (aka. almost all of my DB fields in the table). This seems somewhat kludgy, but it works; however, this is not my primary concern at the moment.

Is it possible to: show which 'orwhere' returned the record into collection? I would like to show (on the results view) what part of the record the string matched.

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

I know I could probably do this on the view (via some more code grepping the filter against the record again), but this option sounds even more laborious.

Thank you!

You could do:

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

And then on view iterate over the ones that matched, but this process may take quiet a bit if you have too many fields…

In your model,

public function getMatch($str) {
   if (strpos($this->bill_address, $str) !== false) {
       return "bill_address";
   } elseif (...) { ... }
}

Not really answers the question, so might be slightly off-topic, but might help you in the future: as you already noticed that what you are doing becomes a bit messy this way, i recommend solving the problem with a different technology. For example the lucene search engine (used by Solr and elastic-search) offers a debug functionality that can be used to show in detail how the score of a returned record was composed, so you can see what part of the query hit.

And its much faster :) https://lucene.apache.org/solr/guide/6_6/common-query-parameters.html#CommonQueryParameters-ThedebugParameter