php Laravel:按类别和位置列出,包括所有子类别和位置

ewm0tg9j  于 2023-11-16  发布在  PHP
关注(0)|答案(1)|浏览(99)

我试图获取基于类别和位置的列表,包括所有子位置以及。我提供了过滤器表单的屏幕截图供参考。
enter image description here
这里的类别表:

| id    | parent_id     | name          |
|----   |-----------    |-------------  |
| 1     | NULL          | Electronics   |
| 2     | 1             | Computers     |
| 3     | 2             | Accessories   |
| 4     | 3             | Keyboards     |

字符串
下面是我的Listing表:

| 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);

uwopmtnx

uwopmtnx1#

在列表类别:

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);

  • 使用此代码希望它将为您工作 *

相关问题