B
B
BonBon Slick2016-01-01 14:57:49
Laravel
BonBon Slick, 2016-01-01 14:57:49

Laravel 5.3 category tree implementation?

How to correctly implement the relationship between the category parent and child to build a tree?

After all, there is only model 1 , the column in the database - parent_id, allows you to display all categories from the category.
I implement it in such a way that I go through all the categories with a recursive function , where the names are displayed and check if there is a parent, if there is, then it goes into it and so on in a circle.
I'm not sure if this is correct, perhaps in Laravel 5.3 this should be done somehow differently?
How would you do?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Arman, 2016-01-01
@BonBonSlick

nested sets ?

M
Muhammad, 2016-01-01
@muhammad_97

Closure Table. Lara package: https://github.com/franzose/ClosureTable . Works very fast even with a large number of categories/subcategories

S
Sergey Sokolov, 2017-06-08
@smolyakov770

elsenot there. We need it as an alternative to the first if'y:

<?php
if(isset($_SESSION['logged_user'])){
    if($_SESSION['logged_user']->id > 0 ){
        echo '<p class="auth">'.$_SESSION['logged_user']->names.'<br>'.$_SESSION['logged_user']->full.' руб</p>';
    }
}
else
{
    echo 123;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question