Laravel - Sub-categories display under Main categories

前端 未结 2 1316
我寻月下人不归
我寻月下人不归 2021-01-15 04:52

Hi there
I have one problem with categories and sub-categories
I have table like:

ID----- Name---- ParentID
1 ------- A ---------

2条回答
  •  粉色の甜心
    2021-01-15 05:40

    Display Sub, Sub & Sub Category -

    Model

    1. Migration the following Category Table:

      Schema::create('categories', function (Blueprint $table) {
          $table->increments('id');
          $table->string('name');
          $table->tinyInteger('parent_id')->default(0);
          $table->timestamps();
      });
      
    2. Input Your Category and Sub-Category Like following Table Structure:

      +-------------+----------------------+----------+
      | id          | name                 |parent_id |
      +-------------+----------------------+----------+
      |           1 | ELECTRONICS          |      0   |
      |           2 | TELEVISIONS          |      1   |
      |           3 | TUBE                 |      2   |
      |           4 | LCD                  |      2   |
      |           5 | PLASMA               |      2   |
      |           6 | PORTABLE ELECTRONICS |      1   |
      |           7 | MP3 PLAYERS          |      6   |
      |           8 | FLASH                |      7   |
      |           9 | CD PLAYERS           |      6   |
      |          10 | 2 WAY RADIOS         |      6   |
      +-------------+----------------------+----------+
      

    Controller

    1. MySQL hierarchical recursive query

      $categories = DB::table('categories as c1')
          ->leftJoin('categories as c2','c2.parent_id','=','c1.id')
          ->leftJoin('categories as c3','c3.parent_id','=','c2.id')
          ->leftJoin('categories as c4','c4.parent_id','=','c3.id')
          ->select('c1.id as c1_id','c1.name as c1_name','c2.id as c2_id','c2.name as c2_name','c3.id as c3_id','c3.name as c3_name','c4.id  as c4_id','c4.name as c4_name')
          ->where('c1.parent_id','=',0)
          ->get();
      

    Output:

    +-----------------+--------------------------+------------------+-----------------+
    |c1_id| c1_name   | c2_id| c2_name           | c3_id| c3_name   | c4_id| c4_name  |
    +-----------------+--------------------------+------------------+-----------------+
    | 1 | ELECTRONICS | 2 | TELEVISIONS          | 3 | TUBE         | NULL  |
    | 1 | ELECTRONICS | 2 | TELEVISIONS          | 4 | LCD          | NULL  |
    | 1 | ELECTRONICS | 2 | TELEVISIONS          | 5 | PLASMA       | NULL  |
    | 1 | ELECTRONICS | 6 | PORTABLE ELECTRONICS | 7 | MP3 PLAYERS  | 8   | FLASH |
    | 1 | ELECTRONICS | 6 | PORTABLE ELECTRONICS | 9 | CD PLAYERS   | NULL  |
    | 1 | ELECTRONICS | 6 | PORTABLE ELECTRONICS | 10| 2 WAY RADIOS | NULL  |
    +-------------+--------------------------------------+--------------+-------+
    
    1. Then, Put $categories values in an Array.

      $all_categories = [];
      $category_c1_id = '';
      $category_c2_id = '';
      $category_c3_id = '';
      $category_c4_id = '';
      $c1_id = -1;
      $c2_id = -1;
      $c3_id = -1;
      $c4_id = -1;
      foreach ($categories as $k=>$category){
          if($category->c1_id != $category_c1_id){
              $c1_id = $c1_id +1;
              $all_categories[$c1_id]['id'] = $category->c1_id;
              $all_categories[$c1_id]['name'] = $category->c1_name;
              $c2_id = -1;
          }
          if($category->c2_id != $category_c2_id && $category->c2_id <> NULL){
              $c2_id = $c2_id +1;
              $all_categories[$c1_id]['children'][$c2_id]['id'] = $category->c2_id;
              $all_categories[$c1_id]['children'][$c2_id]['name'] = $category->c2_name;
              $c3_id = -1;
          }
          if($category->c3_id != $category_c3_id && $category->c3_id <> NULL){
              $c3_id = $c3_id +1;
              $all_categories[$c1_id]['children'][$c2_id]['children'][$c3_id]['id'] = $category->c3_id;
              $all_categories[$c1_id]['children'][$c2_id]['children'][$c3_id]['name'] = $category->c3_name;
              $c4_id = -1;
          }
          if($category->c4_id != $category_c4_id && $category->c3_id <> NULL){
              $c4_id = $c4_id +1;
              $all_categories[$c1_id]['children'][$c2_id]['children'][$c3_id]['children'][$c4_id]['id'] = $category->c4_id;
              $all_categories[$c1_id]['children'][$c2_id]['children'][$c3_id]['children'][$c4_id]['name'] = $category->c4_name;
          }
          $category_c1_id = $category->c1_id;
          $category_c2_id = $category->c2_id;
          $category_c3_id = $category->c3_id;
          $category_c4_id = $category->c4_id;
      }
      return $all_categories;
      

      Output:

      $categories = [
        ['id' => 1, 'name' => 'TV & Home Theather'],
        ['id' => 2, 'name' => 'Tablets & E-Readers'],
        ['id' => 3, 'name' => 'Computers', 'children' => [
          ['id' => 4, 'name' => 'Laptops', 'children' => [
            ['id' => 5, 'name' => 'PC Laptops'],
            ['id' => 6, 'name' => 'Macbooks (Air/Pro)']
          ]],
          ['id' => 7, 'name' => 'Desktops'],
          ['id' => 8, 'name' => 'Monitors']
        ]],
        ['id' => 9, 'name' => 'Cell Phones']
      ];
      

    View

    1. Now Display on Select option on Product upload.


      @foreach($categories as $category) @php($category = (object)$category)
      @if(isset($category->children)) @foreach($category->children as $subcategory) @php($subcategory = (object)$subcategory)
      @if(isset($subcategory->children)) @foreach($subcategory->children as $subsubcategory) @php($subsubcategory = (object)$subsubcategory)
      @endforeach @endif
      @endforeach @endif
      @endforeach

      And ON Menu Option

      
      

提交回复
热议问题