Skip to content

Commit f0302a7

Browse files
committed
Merge pull request #4 from nWidart/patch-2
Refactor signature of listsFlattened method
2 parents 3641d51 + 16c71e8 commit f0302a7

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,16 @@ Of course you will probably want a position column as well. So you will have to
4141
'17' => ' Child 2 Title',
4242
'14' => 'Item 2 Title',
4343
]
44-
```
44+
```
45+
46+
To use it, first call the `nest()` method, followed by the `listsFlattened()` method:
47+
48+
``` php
49+
Model::orderBy('parent_id')->get()->nest()->listsFlattened();
50+
```
51+
52+
By default it will look for a `title` column. You send as first parameter a custom column name:
53+
54+
``` php
55+
Model::orderBy('parent_id')->get()->nest()->listsFlattened('name');
56+
```

src/NestableCollection.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,21 @@ public function nest()
6666
/**
6767
* Recursive function that flatten a nested Collection
6868
* with characters (default is four spaces)
69-
*
69+
*
7070
* @param BaseCollection|null $collection
71+
* @param string $column
7172
* @param integer $level
7273
* @param array &$flattened
73-
* @param string $column
7474
* @param string $indentChars
7575
* @return array
7676
*/
77-
public function listsFlattened(BaseCollection $collection = null, $level = 0, array &$flattened = [], $column = 'title', $indentChars = '    ')
77+
public function listsFlattened($column = 'title', BaseCollection $collection = null, $level = 0, array &$flattened = [], $indentChars = '    ')
7878
{
7979
$collection = $collection ? : $this ;
8080
foreach ($collection as $item) {
8181
$flattened[$item->id] = str_repeat($indentChars, $level) . $item->$column;
8282
if ($item->items) {
83-
$this->listsFlattened($item->items, $level + 1, $flattened);
83+
$this->listsFlattened($column, $item->items, $level + 1, $flattened);
8484
}
8585
}
8686

0 commit comments

Comments
 (0)