maize-tech / laravel-nova-eloquent-sortable
Laravel Nova Eloquent Sortable
Fund package maintenance!
maize-tech
Installs: 1 908
Dependents: 0
Suggesters: 0
Security: 0
Stars: 9
Watchers: 5
Forks: 4
Open Issues: 0
Requires
- php: ^8.0
- illuminate/contracts: ^9.0|^10.0
- laravel/nova: ^4.0
- spatie/eloquent-sortable: ^4.0
- spatie/laravel-package-tools: ^1.14.1
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0|^8.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
README
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!
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.