我想知道如何将此查询转换为雄辩的。我有一些麻烦显示那些“计数”字段。
$resultados = DB::select( DB::raw("
Select * From (
Select
P.*,
count(distinct PP.id) as countProduct,
count(distinct C.id) as countCity,
count(distinct CO.id) as countCountry,
count(distinct CON.id) as countContinent
From
packages P
Inner join citie_package CP
on P.id = CP.package_id
Inner join cities C
on CP.city_id = C.id
Inner Join countries CO
on C.country_id = CO.id
Inner join continents CON
on CO.continent_id = CON.id
Inner Join product_package PP
on P.id = PP.package_id
where
$whereCondition
group by
P.id,
P.name,
P.image,
P.days,
P.operator_id,
P.badge,
P.price,
P.brochure,
P.lodging,
P.itinerary,
P.services,
P.observations,
P.offer,
P.featured,
P.validity,
P.created_at,
P.updated_at,
P.invisible,
P.operationdates
) as pepe
where
$subWhereCondition
"));
现在查询工作正常(在我的localhost上),当我上传到生产时我发现了以下问题:
“SQLSTATE [HY000]:一般错误:2014在其他未缓冲的查询处于活动状态时无法执行查询。请考虑使用PDOStatement :: fetchAll()。或者,如果您的代码只是针对mysql运行,则可以启用查询通过设置PDO :: MYSQL_ATTR_USE_BUFFERED_QUERY属性进行缓冲。
所以我决定再次建造它,但这次使用雄辩。
谢谢!
编辑:
在我尝试这样的事情之前:
$resultado = DB::table('packages')
->join('product_package','packages.id','=','product_package.package_id')
->join('products','products.id','=','product_package.product_id')
->join('citie_package','packages.id','=','citie_package.package_id')
->join('cities','cities.id','=','citie_package.city_id')
->join('countries','countries.id','=','cities.country_id')
->join('continents','continents.id','=','countries.continent_id')
->where(function($resultado) use ($input){
if(isset($input['products'])){
$resultado->whereIn('products.id',$input['products']);
}
if(isset($input['cities'])){
$resultado->whereIn('cities.id',$input['cities']);
}
if(isset($input['countries'])){
$resultado->whereIn('countries.id',$input['countries']);
}
if(isset($input['continents'])){
$resultado->whereIn('continents.id',$input['continents']);
}
if(isset($input['operator'])){
$resultado->whereIn('packages.operator_id',$input['operator']);
}
})
->where('days', '>=', $input['durationFrom'])
->where('days', '<=', $input['durationTo'])
->where('price', '>=', str_replace('$','',str_replace('.','',$input['costFrom'])))
->where('price', '<=', str_replace('$','',str_replace('.','',$input['costTo'])))
- &GT;选择(
阵列(
DB:原料( '包。*'),
DB :: raw('COUNT(distinct cities.id)为count
')
)
) - &GT;得到()
return $ resultado;
使用groupBy!
最后我想通了:D:
这是最终代码
$resultados = Package::
join('product_package','packages.id','=','product_package.package_id')
->join('products','products.id','=','product_package.product_id')
->join('citie_package','packages.id','=','citie_package.package_id')
->join('cities','cities.id','=','citie_package.city_id')
->join('countries','countries.id','=','cities.country_id')
->join('continents','continents.id','=','countries.continent_id')
->where(function($resultado) use ($input){
if(isset($input['products'])){
$resultado->whereIn('products.id',$input['products']);
}
if(isset($input['cities'])){
$resultado->whereIn('cities.id',$input['cities']);
}
if(isset($input['countries'])){
$resultado->whereIn('countries.id',$input['countries']);
}
if(isset($input['continents'])){
$resultado->whereIn('continents.id',$input['continents']);
}
if(isset($input['operator'])){
$resultado->whereIn('packages.operator_id',$input['operator']);
}
})
->groupBy('packages.id')
->where('days', '>=', $input['durationFrom'])
->where('days', '<=', $input['durationTo'])
->where('price', '>=', str_replace('$','',str_replace('.','',$input['costFrom'])))
->where('price', '<=', str_replace('$','',str_replace('.','',$input['costTo'])))
->select( array(
DB::raw('packages.*'),
DB::raw('COUNT(distinct cities.id) as CountCity ,COUNT(distinct continents.id) as CountContinent, COUNT(distinct countries.id) as CountCountry, COUNT(distinct products.id) as CountProduct') ) )
->get();
if(isset($input['cities'])) {
$resultados = $resultados->filter(function ($resultado) use ($input) {
return $resultado->CountCity == count($input['cities']);
});
}
if(isset($input['countries'])) {
$resultados = $resultados->filter(function ($resultado) use ($input) {
return $resultado->CountCountry == count($input['countries']);
});
}
if(isset($input['continents'])) {
$resultados = $resultados->filter(function ($resultado) use ($input) {
return $resultado->CountContinent == count($input['continents']);
});
}
if(isset($input['products'])) {
$resultados = $resultados->filter(function ($resultado) use ($input) {
return $resultado->CountProduct == count($input['products']);
});
}
谢谢!