Laravel 5.2雄辩。查询效果不佳

时间:2016-01-06 08:06:41

标签: php mysql laravel eloquent

请帮忙。我有查询需要在价格中搜索 - 货币($ cur)在数据库中我有 daily_price,hourly_price和货币(GEL,USD)。我写了这个雄辩的查询:

if($request->input('currency') == 'USD'){
        $query->where(function ($qu) use ($request) {
            $qu->where('currency', 'USD')->where(function($q) use ($request){
                $q->whereBetween('daily_price', [$request->input('priceFrom'), $request->input('priceTo')]);
                $q->orWhereBetween('hourly_price', [$request->input('priceFrom'), $request->input('priceTo')]);
            });
        })->orWhere(function ($qu) use ($request, $cur) {
            $qu->where('currency', 'GEL')->where(function($q) use ($request, $cur){
                $q->whereBetween('daily_price', [((int)$request->input('priceFrom'))*$cur, ((int)$request->input('priceTo'))*$cur]);
                $q->orWhereBetween('hourly_price', [((int)$request->input('priceFrom'))*$cur, ((int)$request->input('priceTo'))*$cur]);
            });
        });
    }else{
        $query->where(function ($qu) use ($request) {
            $qu->where('currency', 'GEL')->where(function($q) use ($request){
                $q->whereBetween('daily_price', [$request->input('priceFrom'), $request->input('priceTo')]);
                $q->orWhereBetween('hourly_price', [$request->input('priceFrom'), $request->input('priceTo')]);
            });
        })->orWhere(function ($qu) use ($request, $cur) {
            $qu->where('currency', 'USD')->where(function($q) use ($request, $cur){
                $q->whereBetween('daily_price', [((int)$request->input('priceFrom'))/$cur, ((int)$request->input('priceTo'))/$cur]);
                $q->orWhereBetween('hourly_price', [((int)$request->input('priceFrom'))/$cur, ((int)$request->input('priceTo'))/$cur]);
            });
        });
    }

代码以此查询开头:$query->whereIn('city_id', $arr);

我的数据库中city_id = 5上没有任何内容,但是当我添加货币查询时,它会得到一些(我认为是随机的)行结果...请帮忙。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

if($request->input('currency') == 'USD'){
        $query->where(function ($que) use ($request, $cur) {
            $que->where('currency', 'USD')->where(function($q) use ($request){
                $q->whereBetween('daily_price', [$request->input('priceFrom'), $request->input('priceTo')])
                    ->where('daily_price', '>', 0);
                $q->orWhereBetween('hourly_price', [$request->input('priceFrom'), $request->input('priceTo')])
                    ->where('hourly_price', '>', 0);
            });

            $que->orWhere('currency', 'GEL')->where('daily_price', '>', 0)->where('hourly_price', '>', 0)->where(function($q) use ($request, $cur){
                $q->whereBetween('daily_price', [((int)$request->input('priceFrom'))*$cur, ((int)$request->input('priceTo'))*$cur])
                    ->where('daily_price', '>', 0);;
                $q->orWhereBetween('hourly_price', [((int)$request->input('priceFrom'))*$cur, ((int)$request->input('priceTo'))*$cur])
                    ->where('hourly_price', '>', 0);
            });
        });
    }else{
        $query->where(function ($quer) use ($request, $cur) {
            $quer->where(function ($que) use ($request, $cur){

                $que->where('currency', 'GEL')->where(function($q) use ($request){
                    $q->whereBetween('daily_price', [$request->input('priceFrom'), $request->input('priceTo')])
                        ->where('daily_price', '>', 0);
                    $q->orWhereBetween('hourly_price', [$request->input('priceFrom'), $request->input('priceTo')])
                        ->where('hourly_price', '>', 0);

                });

                $que->orWhere('currency', 'USD')->where(function($q) use ($request, $cur){
                    $q->whereBetween('daily_price', [((int)$request->input('priceFrom'))/$cur, ((int)$request->input('priceTo'))/$cur])
                        ->where('daily_price', '>', 0);
                    $q->orWhereBetween('hourly_price', [((int)$request->input('priceFrom'))/$cur, ((int)$request->input('priceTo'))/$cur])
                        ->where('hourly_price', '>', 0);
                });

            });
        });
    }