我需要遍历数组,并且每个标记都会对数据库进行查询,并按包含最多标记的结果对结果进行排序。
public function search_db(){
$tags=array("I","need","results","promptly");
$results=[];
foreach($tags as $tag){
$result = DB::table('foo')
->where('desc','like','%'.$tag.'%')
->orWhere('name','like','%'.$tag.'%')
->orWhere('url','like','%'.$tag.'%')
->latest()->get();
if(count($result)>0) {
array_push($results,$result);
}
}
$results = collect($results);
$results = $results->flatten();
$results = $results->unique();
$results->values()->all();
}