gpt4 book ai didi

php - Laravel 5.2 Eloquent 。查询效果不佳

转载 作者:行者123 更新时间:2023-11-30 22:28:13 27 4
gpt4 key购买 nike

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

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 上没有任何内容,但是当我添加我的货币查询时,它会得到一些(我认为是随机的)行结果...请帮助。

最佳答案

我找到了解决方案。

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

});
});
}

关于php - Laravel 5.2 Eloquent 。查询效果不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34628315/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com