Git Product home page Git Product logo

laravel-sortable-medialibrary's Introduction

Total Downloads Latest Stable Version License


Changes to the original package

This forked package eliminates the IsSorted Trait from Spatie\MediaLibrary\MediaCollections\Models\Media, so the sorting/ordering part could be made by spatie/eloquent-sortable. I have also added the spatie/eloquent-sortable as a dependency.

Installation

This package uses "spatie/eloquent-sortable": "^3.11" together with "spatie/laravel-medialibrary": "^9.0.0" and it can be installed through Composer:

composer require lartisan/laravel-sortable-medialibrary

In Laravel 5.5 and above the service provider will automatically get registered. In older versions of the framework just add the service provider in config/app.php file:

'providers' => [
    ...
    Spatie\MediaLibrary\MediaLibraryServiceProvider::class,
    Spatie\EloquentSortable\EloquentSortableServiceProvider::class,
];

Optionally you can publish the config files with:

php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="Spatie\EloquentSortable\EloquentSortableServiceProvider" --tag="config"

Usage

To add the intended Eloquent sortable behaviour to your custom Media model you must:

  1. Implement the Spatie\EloquentSortable\Sortable interface.
  2. Use the trait Spatie\EloquentSortable\SortableTrait.
  3. Optionally specify which column will be used as the order column. The default is order_column.
  4. Optionally, if your model/table has a grouping field, you can create a buildSortQuery method at your model.

Example:

...
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;
use Spatie\MediaLibrary\MediaCollections\Models\Media as BaseMedia;

class MyCustomMedia extends BaseMedia implements Sortable
{
    use SortableTrait;
    
    /**
     * Define the column used for sorting
     * @var array
     */
    public $sortable = [
        'order_column_name' => 'order_column',
        'sort_when_creating' => true,
    ];

    /**
     * @return Builder
     */
    public function buildSortQuery(): Builder
    {
        return static::query()
            ->where('collection_name', $this->collection_name);
    }
    ...
}

To add the default sort of the original package behaviour to your custom Media model

You could still use the default IsSorted behaviour of the original package by adding the Spatie\MediaLibrary\MediaCollections\Models\Concerns\IsSorted to your model.

Example:

...
use Spatie\MediaLibrary\MediaCollections\Models\Concerns\IsSorted;
use Spatie\MediaLibrary\MediaCollections\Models\Media as BaseMedia;

class MyCustomMedia extends BaseMedia
{
    use IsSorted;
}

Documentation

The original spatie/laravel-medialibrary package.

The spatie/eloquent-sortable package.

License

The MIT License (MIT). Please see License File for more information.

laravel-sortable-medialibrary's People

Contributors

adrianmrn avatar ahmedofali avatar alexvanderbist avatar alibarthi avatar antonkomarev avatar arubacao avatar bepsvpt avatar bmichotte avatar brendt avatar bstrahija avatar canvural avatar carusogabriel avatar coding-sunshine avatar daniel-werner avatar faustbrian avatar firtzberg avatar francoism90 avatar freekmurze avatar gauravmak avatar gdebrauwer avatar irazasyed avatar matthiasdewinter avatar nicolasbeauvais avatar okipa avatar omranic avatar rhincodon avatar rubenvanassche avatar sebastiandedeyne avatar tvke avatar willemvb avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.