问题描述:

I have next tables in database:

**CATEGORY**

ID CagtegoryName Parent

1 Title 1 0

2 Title 1.1 1

3 Title 1.2 1

4 Title 1.1.1 2

5 Title 1.1.2 2

6 Title 1.1.1.1 4

7 Title 1.1.1.2 4

**ITEMS**

ID Name ... ...

1 Anunt 1

**ItemsCategorys**

ID ItemID CategoryID

1 1 7

The items will be save in ItemsCategorys with CategoryID and ItemID.

The item will be save with last parent ID.

I have the next function in Category Model:

public function items() {

return $this->belongsToMany('App\Models\Items', 'ItemsCategorys', 'CategoryID', 'ItemID');

}

public function childs() {

return $this->hasMany('App\Models\Categorys','Parent', 'ID');

}

How can I make in controller to get all items from all curent subcategories.

Ex:

When I select categeory id = 1, I want to select all items with category id: 2,3,4,5,6,7

When I selected id = 2, I want to select all items with category id: 4,5,6,7

PS: I want to paginate the collection!

相关阅读:
Top