You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.markdown
+50-12
Original file line number
Diff line number
Diff line change
@@ -572,7 +572,9 @@ To install the package, in terminal:
572
572
composer require kalnoy/nestedset
573
573
```
574
574
575
-
### Adding required columns
575
+
### Setting up from scratch
576
+
577
+
#### The schema
576
578
577
579
You can use a method to add needed columns with default names:
578
580
@@ -591,32 +593,68 @@ Schema::table('table', function (Blueprint $table) {
591
593
});
592
594
```
593
595
594
-
If, for some reasons, you want to init everything by yourself, this is preferred schema:
596
+
#### The model
597
+
598
+
Your model should use `Kalnoy\Nestedset\NodeTrait` trait to enable nested sets:
595
599
596
600
```php
597
-
$table->unsignedInteger('_lft');
598
-
$table->unsignedInteger('_rgt');
599
-
$table->unsignedInteger('parent_id')->nullable();
601
+
use Kalnoy\Nestedset\NodeTrait;
600
602
601
-
$table->index([ '_lft', '_rgt', 'parent_id' ]);
603
+
class Foo extends Model {
604
+
use NodeTrait;
605
+
}
602
606
```
603
607
604
-
### The model
608
+
### Migrating existing data
605
609
606
-
Your model should use `Kalnoy\Nestedset\NodeTrait` trait to enable nested sets:
610
+
#### Migrating from other nested set extension
611
+
612
+
If your previous extension used different set of columns, you just need to override
613
+
following methods on your model class:
607
614
608
615
```php
609
-
use Kalnoy\Nestedset\NodeTrait;
616
+
public function getLftName()
617
+
{
618
+
return 'left';
619
+
}
610
620
611
-
class Foo extends Model {
612
-
use NodeTrait;
621
+
public function getRgtName()
622
+
{
623
+
return 'right';
613
624
}
625
+
626
+
public function getParentIdName()
627
+
{
628
+
return 'parent';
629
+
}
630
+
631
+
// Specify parent id attribute mutator
632
+
public function setParentAttribute($value)
633
+
{
634
+
$this->setParentIdAttribute($value);
635
+
}
636
+
```
637
+
638
+
#### Migrating from basic parentage info
639
+
640
+
If your tree contains `parent_id` info, you need to add two columns to your schema:
641
+
642
+
```php
643
+
$table->unsignedInteger('_lft');
644
+
$table->unsignedInteger('_rgt');
645
+
```
646
+
647
+
After [setting up your model](#the-model) you only need to fix the tree to fill
648
+
`_lft` and `_rgt` columns:
649
+
650
+
```php
651
+
MyModel::fixTree();
614
652
```
615
653
616
654
License
617
655
=======
618
656
619
-
Copyright (c) 2014 Alexander Kalnoy
657
+
Copyright (c) 2016 Alexander Kalnoy
620
658
621
659
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
0 commit comments