Giter VIP home page Giter VIP logo

nova-sortable's Introduction

Nova Sortable

Latest Version on Packagist Total Downloads

This Laravel Nova package allows you to reorder models in a Nova resource's index view using drag & drop.

Uses Spatie's eloquent-sortable under the hood.

Requirements

  • php: >=7.3
  • laravel/nova: ^3.0

Features

  • Drag & drop reorder (on either Index view or HasMany view)
  • BelongsTo/MorphsTo reorder support w/ pivot tables
  • Move to start and end arrows (makes item first/last)
  • Everything from eloquent-sortable
  • Localization

Screenshots

Sortable

Installation

Install the package in a Laravel Nova project via Composer:

# Install package
composer require optimistdigital/nova-sortable

Usage

Create migration

Add an order field to the model using Laravel migrations:

// Add order column to the model
Schema::table('some_model', function (Blueprint $table) {
  $table->integer('sort_order');
});

// Set default sort order (just copy ID to sort order)
DB::statement('UPDATE some_model SET sort_order = id');

Implement eloquent-sortable

Implement the Spatie's eloquent-sortable interface and apply the trait:

use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;

class SomeModel extends Eloquent implements Sortable
{
  use SortableTrait;

  public $sortable = [
    'order_column_name' => 'sort_order',
    'sort_when_creating' => true,
  ];

  ...
}

When the model does not have a sortable configuration, the default eloquent-sortable configuration will be used.

Apply HasSortableRows to Nova resource

Apply HasSortableRows trait from this package on the Resource:

use OptimistDigital\NovaSortable\Traits\HasSortableRows;

class MyResource extends Resource
{
  use HasSortableRows;

  ...
}

NB! This overrides the indexQuery() method.

Disallowing sorting on a per-request/resource basis

You can disable sorting on a per-request or per-resource basis by overriding the canSort() on the Resource method like so:

public static function canSort(NovaRequest $request, $resource)
{
  // Do whatever here, ie:
  // return user()->isAdmin();
  // return $resource->id !== 5;
  return true;
}

Sorting on HasMany relationship

NB! The resource can only be sorted on either the Index view or the HasMany list view, but not both!

Sorting on HasMany is simple. Add 'sort_on_has_many' => true to the $sortable array on the model. Like so:

public $sortable = [
  'order_column_name' => 'sort_order',
  'sort_when_creating' => true,
  'sort_on_has_many' => true,
];

The sort on has many configuration can be apply in a per model basis or it can be added in the eloquent-sortable configuration for all the models.

return [

    // Spatie sortable configuration

    /**
     * Add sort on has many in all the models.
     **/
    'sort_on_has_many' => true,
];

Sorting on ManyToMany relationships

Sorting on BelongsToMany and MorphToMany relationships is available, but requires special steps.

See the documentation here: Sorting ManyToMany relationships (w/ pivot table).

Localization

The translation file(s) can be published by using the following publish command:

php artisan vendor:publish --provider="OptimistDigital\NovaSortable\ToolServiceProvider" --tag="translations"

You can add your translations to resources/lang/vendor/nova-sortable/ by creating a new translations file with the locale name (ie et.json) and copying the JSON from the existing en.json.

Credits

License

Nova Sortable is open-sourced software licensed under the MIT license.

nova-sortable's People

Contributors

tarpsvo avatar pzmarzly avatar franciscokraefft avatar jnewbon avatar faridaghili avatar gerardnll avatar mynetx avatar shaffe-fr avatar naoray avatar marispro avatar mikaelpopowicz avatar everestmx avatar newtongamajr avatar thesetj avatar stevelacey avatar toonvandenbos avatar eduardoesternon avatar

Watchers

James Cloos 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.