PHP code example of codeaxion / nestifyx

1. Go to this page and download the library: Download codeaxion/nestifyx library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codeaxion / nestifyx example snippets


  php artisan migrate;

  use CodeAxion\NestifyX\Facades\NestifyX;

   use CodeAxion\NestifyX\Facades\NestifyX;

   $categories = Category::orderByRaw('-position DESC')->get();

   $categories = NestifyX::nestTree($categories);
  

  use CodeAxion\NestifyX\Facades\NestifyX;

  $categories = Category::orderByRaw('-position DESC')->get(); //Sort by children with it's position (RECOMMENDED after sorting is done by jstree)
  //OR
  $categories = Category::get() //Sort by only children;

  //will return original database collection 
  $categories = NestifyX::setIndent('|--')->sortChildren($categories); //fetch all categories and subcategories
  //OR
  $categories = NestifyX::setIndent('|--')->sortChildren($categories,5) //Pass different category id if you want their children of (refer to 2nd example)

  // In view
  @foreach($categories as $category)
  <div> {{$category->indent}} {{$category->name}} </div>
  @endforeach

  //Will result
  /** 
  Electronics
  |-- Mobiles
  |-- |-- All Mobiles
  |-- Headphones
  |-- |-- All Headphones
  |-- Gaming
  |-- |-- Gaming Laptops
  |-- |-- Business Laptops
  |-- Laptops
  |-- |-- All Laptops
  |-- |-- Apple Laptops
  |-- |-- Microsoft Laptops
  */

    use CodeAxion\NestifyX\Facades\NestifyX;

    $categories = \App\Models\Category::orderByRaw('-position DESC')->get();

    //fetch all categories and subcategories (CASE 1)
    $categories = NestifyX::setIndent('|--')->listsFlattened($categories); 

    //OR
    //pass different category id if you want their children of (CASE 2)
    $categories = NestifyX::setIndent('|--')->listsFlattened($categories,5);
    //OR
    ////if your column name is different (default will be name)
    $categories = NestifyX::setIndent('|--')->setColumn('title')->listsFlattened($categories); 

    dd($categories);

    //Result (CASE 1):
    /**
      #items: array:7 [▼
        4 => "Electronics"
        5 => "|--Laptops"
        9 => "|--|--All Laptops"
        7 => "|--Mobiles"
        10 => "|--|--All Mobiles"
        8 => "|--Headphones"
        11 => "|--|--All Headphones"
      ]
    */

    //Result (CASE 2):
    /**
      #items: array:7 [▼
        5 => "Laptops"
        9 => "|-- All Laptops"
      ]
    */



    $categories = \App\Models\Category::orderByRaw('-position DESC')->get();
 
    $categories = NestifyX::setIndent(' \ ')->generateBreadCrumbs($categories);

    //will return flattened Result:
    
    array:7 [▼ // routes\web.php:27
      4 => "Electronics"
      5 => "Electronics \ Laptops"
      9 => "Electronics \ Laptops \ All Laptops"
      7 => "Electronics \ Mobiles"
      10 => "Electronics \ Mobiles \ All Mobiles"
      8 => "Electronics \ Headphones"
      11 => "Electronics \ Headphones \ All Headphones"
    ]


    $categories = Category::get();

    $categories = NestifyX::generateCurrentBreadCrumbs($categories,5); //2nd param: child id 

    array:2 [▼ 
      4 => array:2 [▼
        "name" => "Electronics"
        "category" => array:8 [▼
          "id" => 4
          "name" => "Electronics"
          "parent_id" => null
          "created_at" => "2024-02-18T13:37:52.000000Z"
          "updated_at" => "2024-02-22T16:40:00.000000Z"
        ]
      ]
      5 => array:2 [▼
        "name" => "Laptops"
        "category" => array:8 [▼
          "id" => 5
          "name" => "Laptops"
          "parent_id" => 4
          "created_at" => "2024-02-18T13:43:26.000000Z"
          "updated_at" => "2024-02-22T16:40:00.000000Z"
        ]
      ]
    ]


    $categories = Category::get();
   
    $categories = NestifyX::collectParentIds($categories,11);



    $categories = Category::get();

    $categories = NestifyX::collectChildIds($categories,4);


php artisan vendor:publish --provider="CodeAxion\NestifyX\NestifyXServiceProvider" --tag="config"


php artisan vendor:publish --provider="CodeAxion\NestifyX\NestifyXServiceProvider" --tag="migrations"

html

  <!-- In view -->
 <select class="border-gray-400" name="" id="">
      @foreach ($categories as $id => $name)
      <option value="{{$id}}"> {{$name}} </option>
      @endforeach
  </select>