Last active
October 15, 2022 09:59
-
-
Save Lemmings19/9a9a9b934ce0d28726c0a4dbe23d39e5 to your computer and use it in GitHub Desktop.
How to create a recursive list in a Blade. (Laravel)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
class TestController extends Controller | |
{ | |
/** | |
* This is just a quick example of how to create a recursive list of items | |
* presented in a list in HTML using Laravel's Blade views. | |
* | |
* Also presenting long or short names for those items. | |
* Also presenting checkboxes for some of those items. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function index(Request $request) | |
{ | |
$foods = array( | |
0 => array( | |
'id' => 1, | |
'full_name' => 'plants', | |
'short_name' => null, | |
'use_short_name' => 0, | |
'is_selectable' => 0, | |
'description' => null, | |
'children' => array ( | |
0 => array( | |
'id' => 2, | |
'full_name' => '10', | |
'short_name' => null, | |
'use_short_name' => 0, | |
'is_selectable' => 0, | |
'description' => null, | |
'children' => array ( | |
0 => array( | |
'id' => 3, | |
'full_name' => 'tomato', | |
'short_name' => 'tom', | |
'use_short_name' => 1, | |
'is_selectable' => 1, | |
'description' => 'www.tomato.com', | |
'children' => null, | |
), | |
1 => array( | |
'id' => 4, | |
'full_name' => 'carrot', | |
'short_name' => null, | |
'use_short_name' => 0, | |
'is_selectable' => 1, | |
'description' => 'www.carrot.com', | |
'children' => null, | |
) | |
) | |
), | |
1 => array( | |
'id' => 5, | |
'full_name' => '20', | |
'short_name' => null, | |
'use_short_name' => 0, | |
'is_selectable' => 0, | |
'description' => null, | |
'children' => array ( | |
0 => array( | |
'id' => 6, | |
'full_name' => 'kiwi', | |
'short_name' => null, | |
'use_short_name' => 0, | |
'is_selectable' => 1, | |
'description' => 'www.kiwi.com', | |
'children' => null, | |
), | |
1 => array( | |
'id' => 7, | |
'full_name' => 'watermelon', | |
'short_name' => 'watts', | |
'use_short_name' => 1, | |
'is_selectable' => 1, | |
'description' => 'www.watermelon.com', | |
'children' => null, | |
) | |
) | |
) | |
) | |
), | |
1 => array( | |
'id' => 8, | |
'full_name' => 'meats', | |
'short_name' => null, | |
'use_short_name' => 0, | |
'is_selectable' => 0, | |
'description' => null, | |
'children' => array ( | |
0 => array( | |
'id' => 9, | |
'full_name' => 'mammal', | |
'short_name' => null, | |
'use_short_name' => 0, | |
'is_selectable' => 1, | |
'description' => 'www.mammals.com', | |
'children' => null, | |
), | |
1 => array( | |
'id' => 10, | |
'full_name' => 'fish', | |
'short_name' => null, | |
'use_short_name' => 0, | |
'is_selectable' => 1, | |
'description' => 'www.fish.com', | |
'children' => null, | |
) | |
) | |
), | |
); | |
return view('test', ['foods' => $foods]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://i.imgur.com/8P8qodc.png | |
Looks something like this: | |
plants | |
10 | |
[ ] tom | |
[ ] carrot | |
20 | |
[ ] kiwi | |
[ ] watts | |
meats | |
[ ] mammal | |
[ ] fish | |
HTML: | |
<ul> | |
<li> | |
plants | |
<ul> | |
<li> | |
10 | |
<ul> | |
<li> | |
<input type="checkbox" name="foods" value="3"> tom | |
</li> | |
<li> | |
<input type="checkbox" name="foods" value="4"> carrot | |
</li> | |
</ul> | |
</li> | |
<li> | |
20 | |
<ul> | |
<li> | |
<input type="checkbox" name="foods" value="6"> kiwi | |
</li> | |
<li> | |
<input type="checkbox" name="foods" value="7"> watts | |
</li> | |
</ul> | |
</li> | |
</ul> | |
</li> | |
<li> | |
meats | |
<ul> | |
<li> | |
<input type="checkbox" name="foods" value="9"> mammal | |
</li> | |
<li> | |
<input type="checkbox" name="foods" value="10"> fish | |
</li> | |
</ul> | |
</li> | |
</ul> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ $food['use_short_name'] ? $food['short_name'] : $food['full_name'] }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<li> | |
@if($food['is_selectable']) | |
<input type="checkbox" name="foods" value="{{ $food['id'] }}"> @include('test-item-name') | |
@else | |
@include('test-item-name') | |
@endif | |
@if ($food['children']) | |
<ul> | |
@each('test-item', $food['children'], 'food') | |
</ul> | |
@endif | |
</li> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="container"> | |
<div class="row"> | |
<div class="col-md-8 col-md-offset-2"> | |
<div class="panel panel-default"> | |
<div class="panel-heading">Test!</div> | |
<div class="panel-body"> | |
@if($foods) | |
<ul> | |
@each('test-item', $foods, 'food') | |
</ul> | |
@endif | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that
@each
will only ever accept one parameter. And it doesn't currently have access to variables that are in the blade's scope.For more flexibility, use
@foreach
and@include
to have access to variables in the blade's scope, and pass as many variables as you'd like.eg.