提问人:Tajmmal Hussan 提问时间:11/10/2023 更新时间:11/15/2023 访问量:27
Laravel:按类别和位置检索列表,包括所有子类别和位置
Laravel: Retrieve Listing by category and location, including all child categories and locations
问:
我正在尝试根据类别和位置获取列表,也包括所有子位置。我提供了过滤器表单的屏幕截图以供参考。
表 这是类别表:
| id | parent_id | name |
|---- |----------- |------------- |
| 1 | NULL | Electronics |
| 2 | 1 | Computers |
| 3 | 2 | Accessories |
| 4 | 3 | Keyboards |
这是我的列表表:
| id | category_id | name |
|---- |------------- |----------- |
| 1 | 2 | Product 1 |
| 2 | 3 | Product 2 |
| 3 | 4 | Product 3 |
这是位置表:
| id | parent_id | name |
|---- |----------- |------------- |
| 1 | NULL | Texas |
| 2 | 1 | London |
| 3 | 2 | United States |
| 4 | 3 | Canada |
在类别和位置模型中使用以下代码:
public function childrenRecursive()
{
return $this->children()->with('childrenRecursive', 'rListing');
}
当用户选择某个位置时,系统将检索其所有子位置。同样,当用户同时选择位置和类别时,系统应根据位置搜索类别,包括所有子位置,而不是专注于特定类别。
使用此代码,但它适用于特定的类别和位置
$listing_items = DB::table('listings');
if($request->text != '') {
$listing_items = $listing_items->where('listings.listing_name','LIKE', '%'.$request->text.'%');
}
if($request->category != '') {
$listing_items = $listing_items->where('listings.listing_category_id',$request->category);
}
if($request->location != '') {
$listing_items = $listing_items->where('listings.listing_location_id',$request->location);
}
$listing_items = $listing_items->join('listing_categories', 'listings.listing_category_id','=', 'listing_categories.id');
$listing_items = $listing_items->join('listing_locations', 'listings.listing_location_id','=', 'listing_locations.id');
$listing_items = $listing_items->where('listings.listing_status','Active');
$listing_items = $listing_items->select('listings.*',
'listing_categories.listing_category_name',
'listing_categories.listing_category_slug',
'listing_locations.listing_location_name',
'listing_locations.listing_location_slug');
$listing_items = $listing_items->paginate(4);
答:
0赞
Tajmmal Hussan
11/10/2023
#1
在 ListingCategory:
public function getAllChildCategoryIds()
{
if($this->childrenRecursive->isNotEmpty())
{
$childIds = $this->childrenRecursive->pluck('id')->all();
foreach ($this->childrenRecursive as $child) {
$childIds = array_merge($childIds, $child->getAllChildCategoryIds());
}
$childIds = array_merge($childIds, [$this->id]);
}else{
$childIds = [$this->id];
}
return $childIds;
}
然后你可以像这样使用这个函数:
$listing_items = DB::table('listings');
if($request->text != '') {
$listing_items = $listing_items->where('listings.listing_name','LIKE', '%'.$request->text.'%');
}
if($request->category != '') {
$category = ListingCategory::with(['rListing', 'childrenRecursive', 'childrenRecursive.rListing'])->find($request->category);
$categoryIds = $category->getAllChildCategoryIds();
$listing_items->whereIn('listings.listing_category_id', $categoryIds);
}
if($request->location != '') {
$location = ListingLocation::with(['rListing', 'childrenRecursive', 'childrenRecursive.rListing'])->find($request->location);
$locationIds = $location->getAllChildLocationIds();
$listing_items->whereIn('listings.listing_location_id', $locationIds);
}
$listing_items->join('listing_categories', 'listings.listing_category_id', '=', 'listing_categories.id');
$listing_items->join('listing_locations', 'listings.listing_location_id', '=', 'listing_locations.id');
$listing_items = $listing_items->where('listings.listing_status','Active');
$listing_items = $listing_items->select('listings.*',
'listing_categories.listing_category_name',
'listing_categories.listing_category_slug',
'listing_locations.listing_location_name',
'listing_locations.listing_location_slug');
$listing_items = $listing_items->paginate(4);
使用此代码希望它对您有用
评论