简体   繁体   English

Laravel 5.2雄辩。 查询效果不佳

[英]Laravel 5.2 Eloquent. Query is not working well

Please help. 请帮忙。 I have query where I need to search with price - currency ($cur) in DB I have daily_price, hourly_price and currency (GEL, USD). 我在查询中需要用价格搜索-DB中的货币($ cur),我有daily_price,hourly_price和货币 (GEL,USD)。 I have written this Eloquent query: 我写了这个雄辩的查询:

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]);
            });
        });
    }

the code starts with this query: $query->whereIn('city_id', $arr); 代码以以下查询开头: $query->whereIn('city_id', $arr);

I don't have anything on city_id = 5 in my DB, but when I add my currency query it gets me some (I think random) result of rows... Please help. 我的数据库中没有city_id = 5任何内容,但是当我添加货币查询时,会得到一些city_id = 5结果(我认为是随机的)……请帮助。

I have found solution. 我找到了解决方案。

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);
                });

            });
        });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM