PHP code example of alexcrawford / lexorank-sortable

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

    

alexcrawford / lexorank-sortable example snippets


// schema builder example
public function up()
{
    Schema::create('articles', function (Blueprint $table) {
        // ... other fields ...
        $table->string('position'); // Your model must have position field
    });
}

// MySQL/MariaDB - REQUIRED for correct sorting
public function up()
{
    Schema::create('articles', function (Blueprint $table) {
        // ... other fields ...
        $table->string('position')->charset('utf8mb4')->collation('utf8mb4_bin');
        // or use binary type for guaranteed ASCII sorting:
        // $table->binary('position', 255);
    });
}

class Article extends Model
{
    use \AlexCrawford\Sortable\SortableTrait;
}

class Article extends Model
{
    use \AlexCrawford\Sortable\SortableTrait;

    protected static $sortableField = 'somefield';
}

$entity = Article::find(1);

$positionEntity = Article::find(10)

$entity->moveAfter($positionEntity);

// if $positionEntity->position is aaa, then $entity->position is aab now

$article = new Article();
$article->title = $faker->sentence(2);
$article->description = $faker->paragraph();
$article->save();

$articles = Article::sorted()->get();

// YourAppServiceProvider

YourModel::deleting(function ($model) {
    $model->next()->decrement('position');
});

protected static $sortableGroupField = 'fieldName';

protected static $sortableGroupField = ['fieldName1','fieldName2'];

Schema::create('post_tag', function (Blueprint $table) {
    $table->unsignedInteger('post_id');
    $table->unsignedInteger('tag_id');
    $table->string('position')->charset('utf8mb4')->collation('utf8mb4_bin'); // MySQL/MariaDB
    // or for SQLite/PostgreSQL: $table->string('position');
    
    $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
    $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
    $table->primary(['post_id', 'tag_id']);
});

class Post extends Model {

    use BelongsToSortedManyTrait;

    public function tags()
    {
        return $this->belongsToSortedMany('\App\Tag');
    }
}

    $post->tags()->save($tag) // or
    $post->tags()->attach($tag->id) // or
    $post->tags()->sync([$tagId1, $tagId2, /* ...tagIds */])

$post->tags; // ordered by position by default

    $post->tags()->moveBefore($entityToMove, $whereToMoveEntity); // or
    $post->tags()->moveAfter($entityToMove, $whereToMoveEntity);

Schema::create('taggables', function (Blueprint $table) {
    $table->unsignedInteger('tag_id');
    $table->string('position')->charset('utf8mb4')->collation('utf8mb4_bin'); // MySQL/MariaDB
    // or for SQLite/PostgreSQL: $table->string('position');
    $table->unsignedInteger('taggable_id');
    $table->string('taggable_type');
    
    $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
    $table->index(['taggable_id', 'taggable_type']);
});

class Post extends Model {

    use MorphToSortedManyTrait;

    public function tags()
    {
        return $this->morphToSortedMany('\App\Tag', 'taggable');
    }
}

'providers' => array(
    // providers...

    'AlexCrawford\Sortable\SortableServiceProvider',
)

'entities' => array(
     'articles' => '\App\Article', // entityNameForUseInRequest => ModelName
     // or
     'articles' => ['entity' => '\App\Article'],
     // or for many to many
     'posts' => [
        'entity' => '\App\Post',
        'relation' => 'tags' // relation name (method name which returns $this->belongsToSortedMany)
     ]
),

Route::post('sort', '\AlexCrawford\Sortable\SortableController@sort');

$validator = \Validator::make(\Input::all(), array(
    'type' => array('ortableEntities))), // entity name, 'articles' in this example
    'positionEntityId' => '))), // entity name, 'articles' in this example
    'positionEntityId' => '
bash
php artisan vendor:publish