我有一个表,称为“庄园”,以及一些特点4分透视表一样的设施,intallations列表等从数据透视表视图的MySQL列添加
当(与Laravel 5个)我想进行搜索(庄园有大约200个田地和5个多重选择)我使用一个叫做“westates”的视图来加快搜索速度。但是当我必须搜索其他特性时,我使用了四个foreach(PHP),并且我跨越了所有结果并与数据透视表比较: - $ models是第一个查询的结果。
$extras = $request->ms_extras_amenities; //Items selected in multiselect
$others = $request->ms_more_features;
$facilities = $request->ms_other_installations;
$lapse = $request->lapse;
$models = $models->filter(function($model) use ($lapse,$extras,$others,$facilities)
{
$haslapse = false;
if(count($lapse)){
foreach($lapse as $item){
$result = \DB::table('rent_period_estates')
->whereRaw("estates_id = $model->id AND rent_period_id = $item")
->count();
if($result != 0) $haslapse = true;
}
}
else
$haslapse = true;
$hasextras = true;
if(count($extras)){
foreach($extras as $extra){
$result = \DB::table('extras_amenities_estates')
->whereRaw("estates_id = $model->id AND extras_id = $extra")
->count();
if($result == 0) $hasextras = false;
}
}
$hasothers = true;
if(count($others)) {
foreach ($others as $other) {
$result = \DB::table('other_chara_estates')
->whereRaw("estates_id = $model->id AND other_chara_id = $other")
->count();
if ($result == 0) $hasothers = false;
}
}
$hasfacilities = true;
if(count($facilities)) {
foreach ($facilities as $facility) {
$result = \DB::table('other_facilities_estates')
->whereRaw("estates_id = $model->id AND other_facilities_id = $facility")
->count();
if ($result == 0) $hasfacilities = false;
}
}
//Estate have to have all items selected in the multiselectd fields
if ($hasothers && $hasextras && $hasfacilities && $haslapse){
return $model;
}
});
现在我有正确的结果,但与7个州测试我会做近300个查询。
我想知道如何创建视图,包括为了将透视条目逗号分隔为文本(这是一个想法),就像在数组中获取结果一样,按顺序对它进行分解([2,1,8,4 ]到“1,2,4,8”)在我看来,所以当我做一个查询时,我只能做一个查询添加: ... AND(westates.others = $ model-> others AND westates .extras = $ model-> extras AND ...)
可能是一个程序可以在MySQL服务器上执行它,但实际上我找不到解决方案。
这里的观点:
"CREATE OR REPLACE VIEW westates AS
SELECT
concat(estates.locale,estates.id) as code,
cities.name as city,
councils.name as council,
states.name as state,
countries.name as country,
concat(cities.name,' (',councils.name,')') as city_council,
estates.*
FROM estates
JOIN countries ON (estates.country_id = countries.id)
JOIN states ON (estates.state_id = states.id)
JOIN councils ON (estates.city_council_id = councils.id)
JOIN cities ON (estates.city_id = cities.id)"