PHP code example of dede / lexorank

1. Go to this page and download the library: Download dede/lexorank 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/ */

    

dede / lexorank example snippets


    use Dede\Lexorank\LexoRankTrait;

    class CustomItem extends Model
    {
        use LexoRankTrait;

        // Optionally override the default sortable field
        protected static $sortableField = custom_position;
    }
    


    //example custom applySortableGroup 
    protected static function applySortableGroup(QueryBuilder $query, Model $model)
    {
        if (property_exists($model, 'category_id') && property_exists($model, 'active')) {
            $query->where('category_id', $x)
                  ->where('active',1);
        }

        return $query;
    }
    

    Schema::table(custom_items, function (Blueprint $table) {
        $table->string('position')->nullable();

        $table->index('position'); // recommend
    });
    

    $items = CustomItem::sorted()->get();
    

    $firstItem->moveAfter($secondItem);
    

    $firstItem->moveBefore($secondItem);
    

    CustomItem::resetPositions();
    

use App\Models\CustomItem;

// Creating a new item
$item = CustomItem::create([
    name => New Item,
    // Any other attributes
]);

// Reordering an item
$item1 = CustomItem::find(1);
$item2 = CustomItem::find(2);

// Move item1 before item2
$item1->moveBefore($item2);

// Move item1 after item2
$item1->moveAfter($item2);

// Get next items (with a limit)
$nextItems = $item1->getNext(5);

// Get previous items (with a limit)
$previousItems = $item1->getPrevious(5);

class CustomItem extends Model
{
    use LexoRankTrait;

    // Override the default sortable field to rank
    protected static $sortableField = rank;
}