Skip to content

Commit 26fc2c3

Browse files
committed
Update README.markdown
1 parent e77dab3 commit 26fc2c3

File tree

1 file changed

+50
-12
lines changed

1 file changed

+50
-12
lines changed

README.markdown

+50-12
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,9 @@ To install the package, in terminal:
572572
composer require kalnoy/nestedset
573573
```
574574

575-
### Adding required columns
575+
### Setting up from scratch
576+
577+
#### The schema
576578

577579
You can use a method to add needed columns with default names:
578580

@@ -591,32 +593,68 @@ Schema::table('table', function (Blueprint $table) {
591593
});
592594
```
593595

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

596600
```php
597-
$table->unsignedInteger('_lft');
598-
$table->unsignedInteger('_rgt');
599-
$table->unsignedInteger('parent_id')->nullable();
601+
use Kalnoy\Nestedset\NodeTrait;
600602

601-
$table->index([ '_lft', '_rgt', 'parent_id' ]);
603+
class Foo extends Model {
604+
use NodeTrait;
605+
}
602606
```
603607

604-
### The model
608+
### Migrating existing data
605609

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

608615
```php
609-
use Kalnoy\Nestedset\NodeTrait;
616+
public function getLftName()
617+
{
618+
return 'left';
619+
}
610620

611-
class Foo extends Model {
612-
use NodeTrait;
621+
public function getRgtName()
622+
{
623+
return 'right';
613624
}
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();
614652
```
615653

616654
License
617655
=======
618656

619-
Copyright (c) 2014 Alexander Kalnoy
657+
Copyright (c) 2016 Alexander Kalnoy
620658

621659
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:
622660

0 commit comments

Comments
 (0)