gpt4 book ai didi

php - Laravel 根据用户选择返回结果

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

我有以下内容

HTML

<input type="checkbox" name="symbols[]" value="1" />
<input type="checkbox" name="symbols[]" value="2" />
<input type="checkbox" name="symbols[]" value="3" />
etc...
<select name="city">
<option value="Dublin">Dublin</option>
<option value="Cork">Cork</option>
etc ...
</select>

拉维

return DB::table('restaurants')
->select('restaurants.id as restaurantId',
'restaurants.image_id as imageId',
'restaurants.name as restaurantName',
'restaurants.slug as restaurantSlug',
'restaurants.description as restaurantDescription')
->leftJoin('restaurants_symbols', 'restaurants_symbols.restaurant_id', '=', 'restaurants.id')
->leftJoin('restaurants_locations', 'restaurants_locations.restaurant_id', '=', 'restaurants.id')
->groupBy('restaurants_symbols.restaurant_id')
->where(function($q) use ($input) {
$q->whereIn('restaurants_symbols.symbol_id', $input['symbol']);

if(!empty($input['city'])) {
$q->where('restaurants_locations.city', $input['city']);
}
})
->get();

如果用户选中 value1 和 value2,则查询返回所有包含 value1 或 value2 的餐厅,所以我想要实现的是仅返回具有仅表示 value1 和 value2 的符号的餐厅。

有什么建议吗?我在想也许可以按原样返回值,然后在 foreach 循环中过滤它们,但我不知道这是否可能,如果是的话,我看不出如何去做的逻辑,对此有任何想法?

最佳答案

试试这个..

对 where 条件使用 forloop。它的返回值 1 和 value2 适合你的情况

$symbol=$input['symbol'];
return DB::table('restaurants')
->select('restaurants.id as restaurantId',
'restaurants.image_id as imageId',
'restaurants.name as restaurantName',
'restaurants.slug as restaurantSlug',
'restaurants.description as restaurantDescription')
->leftJoin('restaurants_symbols', 'restaurants_symbols.restaurant_id', '=', 'restaurants.id')
->leftJoin('restaurants_locations', 'restaurants_locations.restaurant_id', '=', 'restaurants.id')
->groupBy('restaurants_symbols.restaurant_id')
->where(function($q) use ($input) {
for($i=0;$i<count($symbol);$i++) //add forloop
{
$q->where('restaurants_symbols.symbol_id', $symbol[$i]);
}

if(!empty($input['city'])) {
$q->where('restaurants_locations.city', $input['city']);
}
})
->get();

关于php - Laravel 根据用户选择返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28872568/

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