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

标签 php mysql search laravel filter

我有以下内容

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/

相关文章:

javascript - 如何在 jQuery 调用函数中从 php 获取错误消息?

php - Docker 工具箱 Xdebug 无法与 PhpStorm 一起使用

php - 如何从去年的MySQL中获取条目

php - Laravel Eloquent 多步拆分查询

mysql - SQL从表1中选择ID并将相同的ID更新到表2

php - $_GET 创建产品页面

PHP Symfony 构建所有 Propel 失败 - MySQL

javascript - 在 Jquery 中搜索无法显示 'data-name' 的结果

mysql - 改进 mysql 中的文件路径搜索

java - SOLR 查询在字段开头时无法正常工作