Skip to content

Commit b7297a0

Browse files
Merge pull request #3 from lazychaser/v4
update from lazychaser
2 parents 01afc29 + e8b6883 commit b7297a0

File tree

8 files changed

+199
-54
lines changed

8 files changed

+199
-54
lines changed

CHANGELOG.markdown

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
### 4.3.2
2+
* Support Laravel 5.6
3+
* Added `nestedSet` and `dropNestedSet` blueprint macros
4+
5+
### 4.3.0
6+
* Support Laravel 5.5
7+
* Added `fixSubtree` and `rebuildSubtree` methods
8+
* Increased performance of tree rebuilding
9+
110
### 4.2.7
211

312
* #217: parent_id, lft and rgt are reset when replicating a node

README.markdown

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
This is a Laravel 4-5 package for working with trees in relational databases.
88

9+
* **Laravel 5.5, 5.6** is supported since v4.3
910
* **Laravel 5.2, 5.3, 5.4** is supported since v4
1011
* **Laravel 5.1** is supported in v3
1112
* **Laravel 4** is supported in v2
1213

1314
Although this project is completely free for use, I appreciate any support!
1415

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)__
1717

1818
__Contents:__
1919

@@ -214,6 +214,16 @@ Node `bar` has no primary key specified, so it will be created.
214214
`$delete` shows whether to delete nodes that are already exists but not present
215215
in `$data`. By default, nodes aren't deleted.
216216

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+
217227
### Retrieving nodes
218228

219229
*In some cases we will use an `$id` variable which is an id of the target node.*
@@ -374,6 +384,9 @@ position.
374384
Various constraints that can be applied to the query builder:
375385

376386
- __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;
377390
- __whereIsAfter($id)__ to get every node (not just siblings) that are after a node
378391
with specified id;
379392
- __whereIsBefore($id)__ to get every node that is before a node with specified id.
@@ -561,17 +574,17 @@ protected function getScopeAttributes()
561574
}
562575
```
563576

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
565578
that are used for scoping:
566579

567580
```php
568581
MenuItem::scoped([ 'menu_id' => 5 ])->withDepth()->get(); // OK
569582
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
571584
```
572585

573586
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:
575588

576589
```php
577590
$node = MenuItem::findOrFail($id);
@@ -585,12 +598,13 @@ To get scoped query builder using instance:
585598
$node->newScopedQuery();
586599
```
587600

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:
590604

591605
```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
594608
```
595609

596610
Requirements
@@ -615,7 +629,21 @@ composer require kalnoy/nestedset
615629

616630
#### The schema
617631

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:
619647

620648
```php
621649
...

composer.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
"require": {
1515
"php": ">=5.5.9",
16-
"illuminate/support": "5.0 - 5.5",
17-
"illuminate/database": "5.0 - 5.5",
18-
"illuminate/events": "5.0 - 5.5"
16+
"illuminate/support": "5.2 - 5.6",
17+
"illuminate/database": "5.2 - 5.6",
18+
"illuminate/events": "5.2 - 5.6"
1919
},
2020

2121
"autoload": {
@@ -29,10 +29,17 @@
2929
},
3030

3131
"minimum-stability": "dev",
32+
"prefer-stable": true,
3233

3334
"extra": {
3435
"branch-alias": {
3536
"dev-master": "v4.2.x-dev"
37+
},
38+
39+
"laravel": {
40+
"providers": [
41+
"Kalnoy\\Nestedset\\NestedSetServiceProvider"
42+
]
3643
}
3744
}
3845
}

src/BaseRelation.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,6 @@ public function getResults()
154154
*/
155155
public function addEagerConstraints(array $models)
156156
{
157-
$model = reset($models);
158-
159-
$this->query = $model->newScopedQuery();
160-
161157
$this->query->whereNested(function (Builder $inner) use ($models) {
162158
// We will use this query in order to apply constraints to the
163159
// base query builder

src/NestedSetServiceProvider.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Kalnoy\Nestedset;
4+
5+
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class NestedSetServiceProvider extends ServiceProvider
9+
{
10+
public function register()
11+
{
12+
Blueprint::macro('nestedSet', function () {
13+
NestedSet::columns($this);
14+
});
15+
16+
Blueprint::macro('dropNestedSet', function () {
17+
NestedSet::dropColumns($this);
18+
});
19+
}
20+
}

0 commit comments

Comments
 (0)