如何通过高级orderBy检索集合?

时间:2019-04-03 12:13:41

标签: php laravel sql-order-by

场景:我想搜索梅赛德斯,但是我忘记了拼写“ Mercedes”的方法,因此我输入了“ mer”,下拉列表的顶部显示“ Nissan Almera”,因为“ Almera”包含“ mer”并且“ Nissan Almera”的ID小于“ Mercedes”,那么我必须滚动直到最底部只能看到“ Mercedes”的结果。

public function getCarModel(Request $request) {
    $term = $request->term;

    $arr = explode(' ', $term);
    $results_collect = [];

    foreach($arr as $key => $str) {
        $model_results = car_models::with('brand')
        ->whereHas('brand', function($q) use($str){
            $q->where('brand', 'LIKE', '%'.$str.'%');
        })
        ->orWhere('year', 'LIKE', '%'.$str.'%')
        ->orWhere('model', 'LIKE', '%'.$str.'%')
        ->orWhere('spec', 'LIKE', '%'.$str.'%')
        ->orWhere('engine', 'LIKE', '%'.$str.'%')
        ->pluck('id');

        $results_collect[$key] = $model_results->toArray();   
    }

    $results_intersect = collect($results_collect[0]);
    foreach ($results_collect as $collect) {
        $results_intersect = $results_intersect->intersect($collect);
    }

    $models = car_models::with('brand')->whereIn('id', $results_intersect)->take(100)->get();
    $auto_complete_results = [];
    foreach ($models as $key => $model) {
        $auto_complete_results[$key] = (object) [
            'value' => $model->id,
            'label' => $model->year.' '.$model->brand->brand.' '.$model->model .' '.$model->spec.' '.$model->engine,
        ];
    }

    return response()->json($auto_complete_results);
}

这是数据库的外观

id | brand_id |型号|规格|引擎|年

1,6,Almera,VL,1.5,2017

2,6,Almera,E,1.5,2014

3,13,A,180 Urban Line(CBU),1.6,2017

brand_id 6 =日产

brand_id 13 =梅赛德斯·奔驰

如果用户键入“ mer”,则此函数将始终返回3条记录,并按ID(1,2,3)排序。我期望的输出是(3,1,2),因为我希望输出先显示品牌结果,然后再按照型号,规格,引擎和年份进行搜索。

1 个答案:

答案 0 :(得分:0)

品牌搜索中,请将代码替换为$q->where('brand', 'LIKE', $str.'%'); 因为您希望搜索键返回的结果应该在响应的开始,而不是整个响应的结果。

public function getCarModel(Request $request) {
        $term = $request->term;

        $arr = explode(' ', $term);
        $results_collect = [];

        foreach($arr as $key => $str) {
            $model_results = car_models::with('brand')
            ->whereHas('brand', function($q) use($str){
                $q->where('brand', 'LIKE', $str.'%');
            })
            ->orWhere('year', 'LIKE', '%'.$str.'%')
            ->orWhere('model', 'LIKE', '%'.$str.'%')
            ->orWhere('spec', 'LIKE', '%'.$str.'%')
            ->orWhere('engine', 'LIKE', '%'.$str.'%')
            ->pluck('id');

            $results_collect[$key] = $model_results->toArray();   
        }

        $results_intersect = collect($results_collect[0]);
        foreach ($results_collect as $collect) {
            $results_intersect = $results_intersect->intersect($collect);
        }

        $models = car_models::with('brand')->whereIn('id', $results_intersect)->take(100)->get();
        $auto_complete_results = [];
        foreach ($models as $key => $model) {
            $auto_complete_results[$key] = (object) [
                'value' => $model->id,
                'label' => $model->year.' '.$model->brand->brand.' '.$model->model .' '.$model->spec.' '.$model->engine,
            ];
        }

        return response()->json($auto_complete_results);
    }

希望这会对您有所帮助。