Skip to content

Instantly share code, notes, and snippets.

@Lemmings19
Last active October 15, 2022 09:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Lemmings19/9a9a9b934ce0d28726c0a4dbe23d39e5 to your computer and use it in GitHub Desktop.
Save Lemmings19/9a9a9b934ce0d28726c0a4dbe23d39e5 to your computer and use it in GitHub Desktop.
How to create a recursive list in a Blade. (Laravel)
<?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]);
}
}
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>
{{ $food['use_short_name'] ? $food['short_name'] : $food['full_name'] }}
<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>
<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>
@Lemmings19
Copy link
Author

Lemmings19 commented Apr 13, 2017

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.

@foreach ($food['children'] as $child)
    @include('test/test-item', ['food' => $child, 'var2' => $var2])
@endforeach

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment