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"
]
*/