简体   繁体   English

使用雄辩的内部雄辩查询遍历数组以获取数据

[英]looping through array to get data using eloquent inside eloquent query

I'm programming a tourism agency website. 我正在为一个旅行社网站编程。 I've hotels in database which have a column called stars which is an integer its value is between 1 to 5. 我在数据库中有一些hotels ,其中有一个名为stars的列,它是一个整数,其值在1到5之间。

I have a form which the user can search hotels based on their stars . 我有一个表格,用户可以根据stars搜索酒店。 The field is checkbox so he can search five stars and four stars hotels together for example. 该字段为checkbox因此他可以一起搜索five starsfour stars酒店。

I read the stars array and redirect the visitor to the search page using this code: 我阅读了stars array并使用以下代码将访问者重定向到搜索页面:

$stars_param = "";
    $i = 1;
    foreach($stars as $star){
        if($i == 1){
            $stars_param .= $star; 
        }else{
            $stars_param .= "&".$star;
        }
        $i++;
    }
    $parameters = "filter=true&&stars=".$stars_param."&&price=".$price."&&area=".$area;
    return \Redirect::to('/hotels-search/?'.$parameters); 

So the url will be like this: 因此,网址将如下所示:

hotels-search?filter=true&&stars=5&1&&price=300&&area=taksim

And in the hotels-search page I read the $_GET , and explode the stars variable and coded this query to fetch data: hotels-search页面中,我读取了$_GET ,并展开了stars变量,并对该查询进行了编码以获取数据:

$stars = $_GET['stars'];
$stars_array = explode('&', $stars);
$price = $_GET['price'];
$area = $_GET['area'];
$this['hotels'] = \Lilessam\Hotels\Models\Hotel::orderBy('id', 'desc')->where(function($query) use($stars_array){
                $i = 1;
                foreach($stars_array as $star){
                    if($i == 1){
                        $query->where('stars', $star);
                    }else{
                        $query->orWhere('stars', $star);
                    }
                    $i++;
                }

                })->where('price', '<=', $price)->whereHas('area', function($query) use($area){
                                $query->where('name', 'LIKE', $area);
                                })->paginate(5);

But with this if I search hotels with 5 and 1 stars, I only get the five stars hotels! 但是如果我搜索5星级和1星级的酒店,那么我只会得到5星级的酒店!

if I search hotels with 4 and 3 stars, I only get the four stars hotels ! 如果我搜索4星级和3星级的酒店,我只会得到4星级的酒店! Other times I get nothing from database at all !! 有时候我根本没有从数据库中获得任何东西!

How can I make a query so I can get hotels with 1 or 3 or 5 stars at the same time?! 我该如何查询,以便同时获得1或3或5星级的酒店?

Why don' you use this 你为什么不使用这个

$this['hotels'] = \Lilessam\Hotels\Models\Hotel::orderBy('id', 'desc')
                    ->whereIn('start',$stars_array)
                    ->where('price', '<=', $price)
                    ->whereHas('area', function($query) use($area){
                            $query->where('name', 'LIKE', $area);
                            })->paginate(5);

get variables are separated by single & , not && . get变量由单个&分隔,而不是&&分隔。 every name value pair has the form:- name=value. 每个名称值对的格式为:-name = value。

Try this. 尝试这个。

$stars_param = implode(",",$stars);
$parameters = "filter=true&stars=".$stars_param."&price=".$price."&area=".$area;
    return \Redirect::to('/hotels-search/?'.$parameters);

Then your URL will be: 那么您的网址将是:

hotels-search?filter=true&stars=5,1&price=300&area=taksim

And in the hotel search page 并在酒店搜索页面

$stars = $_GET['stars'];
$stars_array = explode(',', $stars);
$price = $_GET['price'];
$area = $_GET['area'];

Hope this will help you... 希望这个能对您有所帮助...

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

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