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!
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,
];
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),
];
}
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.
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.
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.
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.
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,
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,
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.