Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于用户选择的Laravel返回结果_Php_Mysql_Search_Laravel_Filter - Fatal编程技术网

Php 基于用户选择的Laravel返回结果

Php 基于用户选择的Laravel返回结果,php,mysql,search,laravel,filter,Php,Mysql,Search,Laravel,Filter,我有以下几点 HTML 如果用户选中value1和value2,查询将返回包含value1或value2的所有餐厅,因此我想实现的是只返回具有表示value1和value2的符号的餐厅 有什么建议吗?我在想,也许可以按原样返回值,然后在foreach循环中过滤它们,但我不知道这是否可行,如果可行,我看不出怎么做的逻辑,对此有什么想法吗?试试这个 将forloop用于where条件。其返回值1和值2适用于您的情况 $symbol=$input['symbol']; return DB::t

我有以下几点

HTML

如果用户选中value1和value2,查询将返回包含value1或value2的所有餐厅,因此我想实现的是只返回具有表示value1和value2的符号的餐厅

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

试试这个

将forloop用于where条件。其返回值1和值2适用于您的情况

$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();
$symbol=$input['symbol'];
return DB::table('restaurants')
->选择('restaurants.id as restaurantId',
“restaurants.image_id as imageId”,
“restaurants.name作为restaurantName”,
“餐厅。作为餐厅的鼻涕虫”,
“restaurants.description as restaurantDescription')
->leftJoin('restaurants\u symbols'、'restaurants\u symbols.restaurant\u id'、'='、'restaurants.id')
->leftJoin('restaurants\u locations'、'restaurants\u locations.restaurant\u id'、'='、'restaurants.id')
->groupBy('restaurants\u symbols.restaurant\u id')
->其中(函数($q)使用($input){
对于($i=0;$iwhere('restaurants\u symbols.symbol\u id',$symbol[$i]);
}
如果(!empty($input['city'])){
$q->where('restaurants\u locations.city',$input['city');
}
})
->get();

感谢您的快速回答。我尝试了它,并在发布$input['symbol']数组(2){[0]=>string(1)“1”[1]=>string(2)“29”}时返回了一个错误“Undefined offset:0”,但如果我选中多个复选框,则不会产生任何结果打印(DB::getQueryLog());在查询之后。它返回什么返回我们执行什么查询。
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();
$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();