maize-tech/laravel-nova-eloquent-sortable

Laravel Nova Eloquent Sortable

3.0.1 2023-11-03 10:03 UTC

This package is auto-updated.

Last update: 2024-11-08 13:35:23 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Easily add inline sortable actions to any resource in Laravel Nova.

This package is heavily based on Spatie's Eloquent Sortable. Please make sure to read its documentation and installation guide before proceeding!

Laravel Nova Eloquent Sortable in action

Installation

You can install the package via composer:

composer require maize-tech/laravel-nova-eloquent-sortable

You can publish the config file with:

php artisan vendor:publish --tag="nova-eloquent-sortable-config"

This is the contents of the published config file:

return [

    /*
    |--------------------------------------------------------------------------
    | See sortable action permission
    |--------------------------------------------------------------------------
    |
    | Here you may specify the fully qualified class name of the invokable class
    | used to determine whether a user can see sortable actions or not.
    | If null, all users who have access to Nova will have the permission.
    |
    */

    'can_see_sortable_action' => null,

    /*
    |--------------------------------------------------------------------------
    | Run sortable action permission
    |--------------------------------------------------------------------------
    |
    | Here you may specify the fully qualified class name of the invokable class
    | used to determine whether a user can sort a given model or not.
    | If null, all users who have access to Nova will have the permission.
    |
    */

    'can_run_sortable_action' => null,

];

Usage

To use the package, add the Maize\NovaEloquentSortable\HasEloquentSortable trait to the nova resource where you want to have marks:

use Laravel\Nova\Resource;
use Maize\NovaEloquentSortable\HasEloquentSortable;

class MyResource extends Resource {
    use HasEloquentSortable;
}

Once done, all you have to do is include all the actions you need for the given resource:

use Maize\NovaEloquentSortable\Actions\MoveOrderDownAction;
use Maize\NovaEloquentSortable\Actions\MoveOrderUpAction;
use Maize\NovaEloquentSortable\Actions\MoveToEndAction;
use Maize\NovaEloquentSortable\Actions\MoveToStartAction;

public function actions(NovaRequest $request)
{
    return [
        MoveOrderDownAction::make(),
        MoveToEndAction::make(),
        MoveOrderUpAction::make(),
        MoveToStartAction::make(),
    ];
}

You can also include the custom OrderColumn field, which allows you to show the order of each entity when indexing them:

use Maize\NovaEloquentSortable\Fields\OrderColumn;

public function fields(NovaRequest $request)
{
    return [
        OrderColumn::new('Order', static::class),
    ];
}

Available Actions

MoveOrderDownAction

The MoveOrderDownAction inline action moves the given model down by a single position.

The action is automatically hidden when the model is already in the last position.

MoveToEndAction

The MoveToEndAction inline action moves the given model to the last position.

The action is automatically hidden when the model is already in the last position.

MoveOrderUpAction

The MoveOrderUpAction inline action moves the given model up by a single position.

The action is automatically hidden when the model is already in the first position.

MoveToStartAction

The MoveToStartAction inline action moves the given model to the first position.

The action is automatically hidden when the model is already in the first position.

Define a custom visibility

By default, all users who have access to Laravel Nova will be able to see all included sort actions.

If you want to restrict their visibility for some users, you can define a custom CanSeeSortableAction invokable class.

Here's an example class checking user's permissions:

use Laravel\Nova\Http\Requests\NovaRequest;

class CanSeeSortableAction
{
    public function __invoke(NovaRequest $request): bool
    {
        return $request->user()->can('sort_models');
    }
}

Once done, all you have to do is reference your custom class in can_see_sortable_action attribute under config/nova-eloquent-sortable.php:

'can_see_sortable_action' => \Path\To\CanSeeSortableAction::class,

Define a custom run permission

By default, all users who have access to Laravel Nova will be able to run all included sort actions.

If you want to restrict the permission for some users, you can define a custom CanRunSortableAction invokable class.

Here's an example class checking user's permissions:

use Illuminate\Database\Eloquent\Model;
use Laravel\Nova\Http\Requests\NovaRequest;

class CanRunSortableAction
{
    public function __invoke(NovaRequest $request, Model $model): bool
    {
        return $request->user()->can('sort_model', $model);
    }
}

Once done, all you have to do is reference your custom class in can_run_sortable_action attribute under config/nova-eloquent-sortable.php:

'can_run_sortable_action' => \Path\To\CanRunSortableAction::class,

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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