I have a question. Before asking this question I have searched a lot but I did not found any workable example, so I have put my question here. I have a concept of parent child relationship with in the same table for example, I have table category like this:
id int auto_increment primary
name varchar
type varchar
parentid int
Now I want to generate select box with multi level option like:
<select class="category" name="category">
<option value="-1">Select Category</select>
<optgroup value="0" label="Parent Tag">
<option value="1">Child Tag</option>
<option value="2">Child Tag</option>
</optgroup>
<optgroup value="3" label="Parent Tag">
<option value="4">Child Tag</option>
<option value="5">Child Tag</option>
</optgroup>
</select>
I also want to generate Menus same as above.
<ul>
<li>Menu 1</li>
<li>
Menu 2
<ul class="dropdown">
<li>Menu 1 of 2</li>
<li>Menu 2 of 2</li>
<li>Menu 3 of 2</li>
</ul>
</li>
<li>Menu 3</li>
<li>
menu 4
<ul class="dropdown">
<li>Menu 1 of 4</li>
<li>Menu 2 of 4</li>
<li>Menu 3 of 4</li>
</ul>
</li>
</ul>
Please friends help to achieve this functionality in laravel 5. Depth for this is not limited and it can be 2, 3, 4; as many as the user wants to add.
Model
class Category extends Model{
...
public function parent()
{
return $this->belongsTo('App\Models\Category', 'parentid');
}
public function children()
{
return $this->hasMany('App\Models\Category', 'parentid');
}
}
In Controller
$categories = Category::with('children')->select('name', 'id','parentid')->where('configtype','=','Category')->get();
return view('admin.category.index',['pageTitle' => 'Category', 'configlist' => $categories]);
In view
?