4-5主要类别和每个主要类别有10-15个子类别。 很少有服务属于少数类别。类别作为序列化数据存储在服务表中,如下所示:
a:2:{i:0;s:1:"1";i:1;s:2:"3";}
a:1:{i:0;s:3:"2";}
a:1:{i:0;s:3:"3";}
a:2:{i:0;s:1:"1";i:1;s:3:"3";}
array(
[0] => array(
[0] => 1,
[1] => 3,
),
[1] => array(
[0] => 2
),
....
....
....
)
将同一主要类别中的每个选定类别分组到一个数组中。此外,类别和子类别的数量也未修复
SELECT * FROM `services` WHERE (`categories` LIKE '%"1"%' OR `categories` LIKE '%"3"%') AND (`categories` LIKE '%"2"%') AND ( ... OR ...) AND ( ... OR ...) ....
我已将此绑定,但这似乎无效。
\DB::table ('services')
->where(function($q) use ($categories) {
foreach($categories as $category) {
$q->where('services.categories', 'like', '%"'.DB::raw($category).'"%');
}
})
答案 0 :(得分:0)
Laravel中带有查询构建器的查询
$queryBuilder = \DB::table('services');
//loop to create dynamic where query
foreach($categories as $category){
//create a query as (condition OR condition)
$queryBuilder->where(function($query) use($category) {
$query->where('categories', 'LIKE', "%" . $category[0] . "%");
//if count is two then adding the or where clause
if(count($category) === 2){
$query->orWhere('categories', 'LIKE', "%" . $category[1] . "%");
}
});
}
}
$result = $queryBuilder->get(); //executing the query to get result