LasseHaslev / LaravelSortable by LasseHaslev

1,164
1
2
Package Data
Maintainer Username: LasseHaslev
Maintainer Contact: lasse@haslev.no (Lasse S. Haslev)
Package Create Date: 2016-11-25
Package Last Update: 2016-11-28
Language: PHP
License: Unknown
Last Refreshed: 2024-11-24 03:01:36
Package Statistics
Total Downloads: 1,164
Monthly Downloads: 34
Daily Downloads: 0
Total Stars: 1
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

lassehaslev/laravel-sortable

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