lassehaslev/laravel-sortable

There is no license information available for the latest version (0.0.1) of this package.

Installs: 1 176

Dependents: 1

Suggesters: 0

Security: 0

Stars: 1

Watchers: 2

Forks: 0

Open Issues: 0

Type:laravel-plugin

0.0.1 2016-11-28 10:46 UTC

This package is not auto-updated.

Last update: 2024-11-23 20:48:09 UTC


README

Add sorting logic to your eloquent models

Install

Run

composer require lassehaslev/laravel-sortable

Create your package and add the following line to providers in config/app.php

    LasseHaslev\LaravelSortable\Providers\ServiceProvider::class,

Usage

Include LasseHaslev\LaravelSortable\Traits\Sortable to models to make it sortable

<?

class TestObject extends Illuminate\Database\Eloquent\Model {
    use LasseHaslev\LaravelSortable\Traits\Sortable;
}

You can also overwrite the column name that holds the sorting value

<?

class TestObject extends Illuminate\Database\Eloquent\Model {
    use LasseHaslev\LaravelSortable\Traits\Sortable;
    protected $sortingColumnName = 'order'; // Default
}

Api

// Get sorted list
$sortedCollection = Object::sorted()->all();

// Move object to position
Object::moveTo( $objectToMove, $position );

// Move to front
Object::moveToFront( $objectToMove );

// Move to back
Object::moveToBack( $objectToMove );

// Increment position by one
Object::incrementPosition( $objectToMove );

// Decrement position by one
Object::decrementPosition( $objectToMove );

Development

composer install
yarn

Runing tests

# Run one time
npm run test

# Automaticly run test on changes
npm run dev