Php 在Laravel或原始SQL中创建基于过滤器的搜索的最佳方法是什么?
我正在进行一个查询,以查找基于不同筛选器的Php 在Laravel或原始SQL中创建基于过滤器的搜索的最佳方法是什么?,php,sql,laravel,Php,Sql,Laravel,我正在进行一个查询,以查找基于不同筛选器的提供的,并需要一些关于如何执行此操作的指导 我的模型结构基本上是:offer->place->categories->filters->filterOptions offer (belongsTo) <-> (hasMany) place place (belongsToMany) <-> (belongsToMany) category place (belongsToMany) <
提供的
,并需要一些关于如何执行此操作的指导
我的模型结构基本上是:offer->place->categories->filters->filterOptions
offer (belongsTo) <-> (hasMany) place
place (belongsToMany) <-> (belongsToMany) category
place (belongsToMany) <-> (belongsToMany) filter
filter (belongsToMany) <-> (belongsToMany) category
filter (hasMany) <-> (belongsToMany) filterOption
到目前为止,我只得到了整个列表,没有过滤
$q = Offer::with([
'place',
'place.categories',
'place.categories.filters',
'place.categories.filters.filterOptions',
])
这将返回JSON对象,如下所示:
[
{
"id": 4,
"place_id": 2,
"place": {
"id": 2,
"categories": [
{
"id": 3,
"name": "Mechanics",
"pivot": {
"place_id": 2,
"category_id": 3
},
"filters": [
{
"id": 5,
"name": "Services",
"pivot": {
"category_id": 3,
"filter_id": 5,
},
"filter_options": [
{
"id": 8,
"name": "Tires",
"filter_id": 5
},
{
"id": 9,
"name": "Painting",
"filter_id": 5
}
]
},
{
"id": 6,
"name": "Amenities",
"pivot": {
"category_id": 3,
"filter_id": 6,
},
"filter_options": [
{
"id": 11,
"name": "Wifi",
"filter_id": 6
},
{
"id": 12,
"name": "Food",
"filter_id": 6
}
]
}
]
}
]
}
}
]
我不知道执行此过滤的起点是什么
$q = Offer::with([
'place',
'place.categories',
'place.categories.filters',
'place.categories.filters.filterOptions',
])
我应该使用雄辩的还是原始的质询?单个查询还是多个查询?联接或分组?通过以下方式解决:
$params = json_decode($request->getContent());
$q = Offer::with([
'place:id,title',
'place.categories:id,name',
'place.categories.filters:id,name',
'place.categories.filters.filterOptions:id,name,filter_id'
])
->whereHas('place.categories', function($q) use ($params){
$q->where('id', $params->category_id);
})
->whereHas('place.categories.filters', function($q) use ($params){
$filters = array_flatten(array_pluck($params->filters, 'filter_id'));
$q->whereIn('id', $filters);
})
->whereHas('place.categories.filters.filterOptions', function($q) use ($params){
$filterOptions = array_flatten(array_pluck($params->filters, 'options.*.option_id'));
$q->whereIn('id', $filterOptions);
})
->select('id','percent','place_id')
->get();
您可能会从我发布的搜索代码中得到一些启发,该代码还使用标记和标记类型来过滤结果。相关代码开始于:
if($tag_types_with_tags){
您是否有laracasts的会员资格?如果有,请检查。他将一些筛选器移动到一个单独的文件中,您可能也可以使用该文件!