how to refactor and make scalable

  laravel, php, refactoring

i used from this structure for save categories in database

+---------+-----------+-----------+
|      id |   name    | parent_id |
+---------+-----------+-----------+
|       1 | Supports  |        0  |    
|       2 | Mobile    |        0  |      
|       3 | Outdoor   |        1  |       
|       4 | Samsung   |        2  |   
|       5 | Cricket   |        3  |  
|       6 | Team A    |        5  |      
+---------+-----------+-----------+

i used this code for create breadcrumbs for category.
this code show last three category

how to refacter this code and show all related category.

please show this

<?php
if ($category->parent_id == 0)
{
    echo <<<EOL
    <ul class="js-selected-category c-content-categories__selected-list" id="breadcrumbs">
    <li class="c-content-categories__selected-category">
        {$category->name}
    </li>
    </ul>
    EOL;
}
else if($category->parent != null)
{
    if (isset($category->parent->parent->name))
    {
        echo <<<EOL
        <ul class="js-selected-category c-content-categories__selected-list" id="breadcrumbs">
        <li class="c-content-categories__selected-category">
            {$category->parent->parent->name}
        </li>
        <li class="c-content-categories__selected-category">
            {$category->parent->name}
        </li>
        <li class="c-content-categories__selected-category">
            {$category->name}
        </li>
        </ul>
        EOL;
    }
    else
    {
        echo <<<EOL
        <ul class="js-selected-category c-content-categories__selected-list" id="breadcrumbs">
        <li class="c-content-categories__selected-category">
            {$category->parent->name}
        </li>
        <li class="c-content-categories__selected-category">
            {$category->name}
        </li>
        </ul>
        EOL;
    }

}

Source: Laravel

Leave a Reply