Basically I am completely stumped with this problem. I have a model in Laravel defined like this
class BlogCategory extends Model
{
protected $table = 'blog_categories';
protected $fillable = [
'parent_id',
'title',
'slug'];
public function parent()
{
return $this->belongsTo('App\Models\BlogCategory','parent_id','blog_category_id');
}
public function children()
{
return $this->hasMany('App\Models\BlogCategory','blog_category_id','parent_id');
}
}
Now as you can see this model can have parent child relationship to itself.
Now what the bootstrap treeview I am using wants is my data to be formatted as follows:
var tree = [
{
text: "Parent 1",
nodes: [
{
text: "Child 1",
nodes: [
{
text: "Grandchild 1"
},
{
text: "Grandchild 2"
}
]
},
{
text: "Child 2"
}
]
},
{
text: "Parent 2"
},
{
text: "Parent 3"
},
{
text: "Parent 4"
},
{
text: "Parent 5"
}
];
How can I achieve the desired outcome?