6
6
7
7
This is a Laravel 4-5 package for working with trees in relational databases.
8
8
9
+ * ** Laravel 5.5, 5.6** is supported since v4.3
9
10
* ** Laravel 5.2, 5.3, 5.4** is supported since v4
10
11
* ** Laravel 5.1** is supported in v3
11
12
* ** Laravel 4** is supported in v2
12
13
13
14
Although this project is completely free for use, I appreciate any support!
14
15
15
- - __ [ Donate via PayPal] ( https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=5TJUM7FYU5VR2 ) __
16
- - My Visa: 4276 0700 1073 4244
16
+ - __ [ Donate via PayPal] ( https://www.paypal.me/lazychaser ) __
17
17
18
18
__ Contents:__
19
19
@@ -214,6 +214,16 @@ Node `bar` has no primary key specified, so it will be created.
214
214
` $delete ` shows whether to delete nodes that are already exists but not present
215
215
in ` $data ` . By default, nodes aren't deleted.
216
216
217
+ ##### Rebuilding a subtree
218
+
219
+ As of 4.2.8 you can rebuild a subtree:
220
+
221
+ ``` php
222
+ Category::rebuildSubtree($root, $data);
223
+ ```
224
+
225
+ This constraints tree rebuilding to descendants of ` $root ` node.
226
+
217
227
### Retrieving nodes
218
228
219
229
* In some cases we will use an ` $id ` variable which is an id of the target node.*
@@ -374,6 +384,9 @@ position.
374
384
Various constraints that can be applied to the query builder:
375
385
376
386
- __ whereIsRoot()__ to get only root nodes;
387
+ - __ hasParent()__ to get non-root nodes;
388
+ - __ whereIsLeaf()__ to get only leaves;
389
+ - __ hasChildren()__ to get non-leave nodes;
377
390
- __ whereIsAfter($id)__ to get every node (not just siblings) that are after a node
378
391
with specified id;
379
392
- __ whereIsBefore($id)__ to get every node that is before a node with specified id.
@@ -561,17 +574,17 @@ protected function getScopeAttributes()
561
574
}
562
575
```
563
576
564
- But now in order to execute some custom query, you need to provide attributes
577
+ But now, in order to execute some custom query, you need to provide attributes
565
578
that are used for scoping:
566
579
567
580
``` php
568
581
MenuItem::scoped([ 'menu_id' => 5 ])->withDepth()->get(); // OK
569
582
MenuItem::descendantsOf($id)->get(); // WRONG: returns nodes from other scope
570
- MenuItem::scoped([ 'menu_id' => 5 ])->fixTree();
583
+ MenuItem::scoped([ 'menu_id' => 5 ])->fixTree(); // OK
571
584
```
572
585
573
586
When requesting nodes using model instance, scopes applied automatically based
574
- on the attributes of that model. See examples :
587
+ on the attributes of that model:
575
588
576
589
``` php
577
590
$node = MenuItem::findOrFail($id);
@@ -585,12 +598,13 @@ To get scoped query builder using instance:
585
598
$node->newScopedQuery();
586
599
```
587
600
588
- Note, that scoping is not required when retrieving model by primary key
589
- (since the key is unique):
601
+ #### Scoping and eager loading
602
+
603
+ Always use scoped query when eager loading:
590
604
591
605
``` php
592
- $node = MenuItem::findOrFail($id); // OK
593
- $node = MenuItem::scoped([ 'menu_id' => 5 ] )->findOrFail(); // OK, but redundant
606
+ MenuItem::scoped([ 'menu_id' => 5])->with('descendants')-> findOrFail($id); // OK
607
+ MenuItem::with('descendants' )->findOrFail($id ); // WRONG
594
608
```
595
609
596
610
Requirements
@@ -615,7 +629,21 @@ composer require kalnoy/nestedset
615
629
616
630
#### The schema
617
631
618
- You can use a method to add needed columns with default names:
632
+ For Laravel 5.5 and above users:
633
+
634
+ ``` php
635
+ Schema::create('table', function (Blueprint $table) {
636
+ ...
637
+ $table->nestedSet();
638
+ });
639
+
640
+ // To drop columns
641
+ Schema::table('table', function (Blueprint $table) {
642
+ $table->dropNestedSet();
643
+ });
644
+ ```
645
+
646
+ For prior Laravel versions:
619
647
620
648
``` php
621
649
...
0 commit comments