medicivn / eloquent-nested-set
Installs: 5 228
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
pkg:composer/medicivn/eloquent-nested-set
Requires
- php: ^8.1
- illuminate/bus: ^9.21
- illuminate/contracts: ^9.21
- illuminate/database: ^9.21
- illuminate/queue: ^9.21
Requires (Dev)
- laravel/legacy-factories: ^1.3
- nunomaduro/collision: ^6.2
- orchestra/testbench: ^7.6
- phpunit/phpunit: ^9.5
- 1.x-dev
- v1.0.10
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-fix/readme
- dev-feature/bulk-update-positions
- dev-feature/fix-hard-coding-id-column
- dev-fix/build-nested-tree
- dev-refactor/reduce-queries-on-update
- dev-feature/fix-tree
- dev-main
- dev-fix/delete-case
- dev-fix/get-primary-key
This package is auto-updated.
Last update: 2025-12-16 22:18:09 UTC
README
Tự động cập nhật lại cây khi tạo mới, cập nhật và xóa một node.
Cách sử dụng:
- Đầu tiên, một Root node với
parent_id=0, left=1, right=2, depth=0cần phải được khởi tạo ở table của bạn - Thêm
use NestedSetModel;vào trong Model tương ứng
use MediciVN\EloquentNestedSet\NestedSetModel; class Category extends Model { use NestedSetModel; /** * ID của Root node * * Mặc định: 1 */ const ROOT_ID = 99999; /** * Tên trường lưu vị trí bên trái của một node * * Mặc định: 'lft' * * Chú ý: kiểu dữ liệu trong Database cần cho phép lưu cả giá trị âm */ const LEFT = 'lft'; /** * Tên trường lưu vị trí bên phải của một node * * Mặc định: 'rgt' * * Chú ý: kiểu dữ liệu trong Database cần cho phép lưu cả giá trị âm */ const RIGHT = 'rgt'; /** * Tên trường lưu ID của node cha * * Mặc định: 'parent_id' */ const PARENT_ID = 'parent_id'; /** * Tên trường lưu giá trị độ sâu - cấp độ của một node * * Mặc định: 'depth' * * Giá trị depth của một node không ảnh hưởng đến việc tính toán left và rigth. * Bạn có thể khởi tạo root node với depth=0, hoặc bất cứ giá trị nào bạn muốn. */ const DEPTH = 'depth'; /** * Bạn có thể triển khai việc cập nhật lại vị trí các nodes với queue nếu lo ngại vấn đề về performance * Queue connection phải được khai báo trong `config/queue.php`. * * Mặc định: null */ const QUEUE_CONNECTION = 'sqs'; /** * Mặc định: null */ const QUEUE = 'your_queue'; /** * Mặc định: true */ const QUEUE_AFTER_COMMIT = true;
Tính năng
-
getTree: lấy tất cả nodes và trả về dưới dạngnested arrayCategory::getTree(); -
getFlatTree: lấy tất cả nodes và trả về dưới dạngflatten array, các nodes con sẽ được sắp xếp ngay sau nút chaCategory::getFlatTree(); -
getLeafNodes: lấy tất cả các nodes lá - các node không có con cháuCategory::getLeafNodes(); -
getAncestors: lấy tất cả các nodes cha - ông (ancestor) của node hiện tại$node = Category::find(123); $node->getAncestors();
-
getAncestorsTree: lấy tất cả các nodes cha - ông (ancestor) của node hiện tại và trả về dưới dạngnested array$node = Category::find(123); $node->getAncestorsTree();
-
getDescendants: lấy tất cả các nodes con cháu (descendant) của node hiện tại$node = Category::find(123); $node->getDescendants();
-
getDescendantsTree: lấy tất cả các nodes con cháu (descendant) của node hiện tại và trả về dưới dạngnested array$node = Category::find(123); $node->getDescendantsTree();
-
parent: lấy node cha của node hiện tại$node = Category::find(123); $node->parent();
-
children: lấy tất cả nodes con của node hiện tại$node = Category::find(123); $node->children();
-
buildNestedTree: build a nested tree base onparent_id$categories = Category::withoutGlobalScope('ignore_root')->get(); $tree = Category::buildNestedTree($categories);
-
fixTree: tính toán lại toàn bộ tree dựa trên parent_idCategory::fixTree();
Query scopes
Root node sẽ dự động bị bỏ qua ở tất cả truy vấn bởi ignore_root global scope.
Để làm việc với root node, sử dụng withoutGlobalScope('ignore_root').
Các query scope khác:
ancestorsdescendantsflattenTreeleafNodes
Chú ý
-
Nếu bạn sử dụng
queuebạn sẽ cần phải sử dụng thêmSoftDelete, bởi vìqueuesẽ thất bại trong trường hợp 1 node bị xóa hoàn toàn, -
Nếu bạn muốn sử dụng SQS-FIFO, hay tham khảo this package.