0

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?

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
raz0r
  • 74
  • 2
  • 10

2 Answers2

0

You don't have to use relations to make this tree. Just select all BlogCategory models and iterate them recursively to make a tree.

For example, you can use answer from here Recursive function to generate multidimensional array from database result and adopt it for your needs.

Community
  • 1
  • 1
Maxim Lanin
  • 4,351
  • 24
  • 32
  • I realize that but my question is still how this can be achieved using Eloquent Relations. – raz0r Oct 23 '15 at 11:31
  • `hasMany` or `belongsTo` methods generate additional select by keys if you are trying to select such relation. So it can't be done via relations without tons of queries. But eloquent relations are helpful for saving node children or changing parents. – Maxim Lanin Oct 23 '15 at 12:11
  • Your answer still basically negates to answer my question which is about the treeview itself not about the approach I plan to use. – raz0r Oct 23 '15 at 14:36
  • I already said. Select all rows, then iterate through them recursively using function from the link as in an example. Or you want me to write full code? – Maxim Lanin Oct 23 '15 at 15:09
  • Let's just rather agree that you are not willing to answer the actual question being asked. I could also have done that but I do not want to. I set up a model relationship for a very specific reason and asked assistance to build a usable array from that and not from iterating through a list. Using the model relationship is what my question is about. – raz0r Oct 26 '15 at 08:07
0

What I had came up with similar situation with L-5.2, if you want to display the categories in multiple level, where single table is maintained for multi-level categories, I guess this will be useful.

id | category_id | name
1      NULL        Main Category
2       1           -Sub-Category
3       1           -Sub Child
4      Null        Second Main
5       4           -Second Sub

In Category model

public function subcategory()
{
    return $this->hasMany(Category::class);
}

then in Controller or where ever you need listing:

$categories = \App\Category::where('category_id', null)->get();

Then, this you can use in you view:

<ul>
@foreach($categories as $category)
    <li>{{ $category->name }}
        @if(count( $category->subcategory) > 0 )
            <ul>
            @foreach($category->subcategory as $subcategory)
                <li>{{ $subcategory->name }}</li>
 {{-- If you want upto 3 level, then you can repeat above code with if condition here --}}
            @endforeach 
            </ul>
        @endif
    </li>                   
@endforeach
</ul>

This will Look something like this in view:

  • Main Category
  • --Sub-Category
  • --Sub Child
  • Second Main
  • --Second Sub

Let me know if this was helpful. Thanks

Tarunn
  • 1,038
  • 3
  • 23
  • 45
  • That will work but then the question of what if children in the tree can also have children undetermined levels deep? Your answer does make sense though as it uses the relationship as I mentioned in the question. – raz0r Apr 10 '17 at 13:21