|
5 | 5 | [](https://github.com/maize-tech/laravel-nova-eloquent-sortable/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) |
6 | 6 | [](https://packagist.org/packages/maize-tech/laravel-nova-eloquent-sortable) |
7 | 7 |
|
8 | | -This package allows you to easily add sortable actions to any model in Laravel Nova. |
| 8 | +Easily add inline sortable actions to any model in Laravel Nova. |
9 | 9 |
|
10 | | -> This project is a work-in-progress. Code and documentation are currently under development and are subject to change. |
| 10 | +>This package is heavily based on Spatie's [Eloquent Sortable](https://github.com/spatie/eloquent-sortable). |
| 11 | +>Please make sure to read its documentation and installation guide before proceeding! |
| 12 | +
|
| 13 | +<p align="center"><img src="/art/preview.gif" alt="Laravel Nova Eloquent Sortable in action"></p> |
11 | 14 |
|
12 | 15 | ## Installation |
13 | 16 |
|
@@ -46,9 +49,104 @@ return [ |
46 | 49 |
|
47 | 50 | ## Usage |
48 | 51 |
|
| 52 | +To use the package, add the `Maize\NovaEloquentSortable\HasEloquentSortable` trait to the nova model where you want to have marks: |
| 53 | + |
| 54 | +```php |
| 55 | +use Laravel\Nova\Resource; |
| 56 | +use Maize\NovaEloquentSortable\HasEloquentSortable; |
| 57 | + |
| 58 | +class Model extends Resource { |
| 59 | + use HasEloquentSortable; |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +Once done, all you have to do is include all the actions you need for the given model: |
| 64 | + |
| 65 | +```php |
| 66 | +use Maize\NovaEloquentSortable\Actions\MoveOrderDownAction; |
| 67 | +use Maize\NovaEloquentSortable\Actions\MoveOrderUpAction; |
| 68 | +use Maize\NovaEloquentSortable\Actions\MoveToEndAction; |
| 69 | +use Maize\NovaEloquentSortable\Actions\MoveToStartAction; |
| 70 | + |
| 71 | +public function actions(NovaRequest $request) |
| 72 | +{ |
| 73 | + return [ |
| 74 | + MoveOrderDownAction::for($this), |
| 75 | + MoveToEndAction::for($this), |
| 76 | + MoveOrderUpAction::for($this), |
| 77 | + MoveToStartAction::for($this), |
| 78 | + ]; |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +You can also include the custom OrderColumn field, which allows you to show the order of each entity when indexing them: |
| 83 | + |
49 | 84 | ```php |
50 | | -$eloquentSortable = new Maize\EloquentSortable(); |
51 | | -echo $eloquentSortable->echoPhrase('Hello, Maize!'); |
| 85 | +use Maize\NovaEloquentSortable\Fields\OrderColumn; |
| 86 | + |
| 87 | +public function fields(NovaRequest $request) |
| 88 | +{ |
| 89 | + return [ |
| 90 | + OrderColumn::new('Order', static::class), |
| 91 | + ]; |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +## Available Actions |
| 96 | + |
| 97 | +- [`MoveOrderDownAction`](#moveorderdown) |
| 98 | +- [`MoveToEndAction`](#movetoend) |
| 99 | +- [`MoveOrderUpAction`](#moveorderup) |
| 100 | +- [`MoveToStartAction`](#movetostart) |
| 101 | + |
| 102 | +### MoveOrderDown |
| 103 | + |
| 104 | +The `MoveOrderDownAction` inline action moves the given model down by a single position. |
| 105 | + |
| 106 | +The action is automatically hidden when the model is already in the last position. |
| 107 | + |
| 108 | +### MoveToEnd |
| 109 | + |
| 110 | +The `MoveToEndAction` inline action moves the given model to the last position. |
| 111 | + |
| 112 | +The action is automatically hidden when the model is already in the last position. |
| 113 | + |
| 114 | +### MoveOrderUp |
| 115 | + |
| 116 | +The `MoveOrderUpAction` inline action moves the given model up by a single position. |
| 117 | + |
| 118 | +The action is automatically hidden when the model is already in the first position. |
| 119 | + |
| 120 | +### MoveToStart |
| 121 | + |
| 122 | +The `MoveToStartAction` inline action moves the given model to the first position. |
| 123 | + |
| 124 | +The action is automatically hidden when the model is already in the first position. |
| 125 | + |
| 126 | +## Define a custom visibility |
| 127 | + |
| 128 | +By default, all users who have access to Laravel Nova will be able to see all included sort actions. |
| 129 | + |
| 130 | +If you want to restrict their visibility to some users, you can define a custom `CanSeeSortableAction` invokable class. |
| 131 | + |
| 132 | +Here's an example class checking user's permissions: |
| 133 | + |
| 134 | +```php |
| 135 | +use Laravel\Nova\Http\Requests\NovaRequest; |
| 136 | + |
| 137 | +class CanSeeSortableAction |
| 138 | +{ |
| 139 | + public function __invoke(NovaRequest $request, $model = null, $resource = null): bool |
| 140 | + { |
| 141 | + return $request->user()->can('sort_models'); |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +Once done, all you have to do is reference your custom class in `can_see_sortable_action` attribute under `config/nova-eloquent-sortable.php`: |
| 147 | + |
| 148 | +``` php |
| 149 | +'can_see_sortable_action' => \Path\To\CanSeeSortableAction::class, |
52 | 150 | ``` |
53 | 151 |
|
54 | 152 | ## Testing |
|
0 commit comments