molbal / nova-order-field
A Laravel Nova field by michielkempen.
v2.1.1
2022-08-03 11:43 UTC
Requires
- php: >=7.1.0
- laravel/nova: ^2.0|^3.0|^4.0
- spatie/eloquent-sortable: ^3.4|^4.0
This package is auto-updated.
Last update: 2024-10-30 01:32:08 UTC
README
Description
A field that adds reordering functionality to your Laravel Nova resource's index using the eloquent-sortable package by Spatie.
Demo
Installation
This package can be installed through Composer.
composer require michielkempen/nova-order-field
Usage
- Follow the usage instructions on the eloquent-sortable repository to make your model sortable.
- Add the
MichielKempen\NovaOrderField\Orderable
trait to your Nova Resource. - Add a public static property called
$defaultOrderField
to your resource, containing your order column. - Add the
OrderField
to your Nova Resourcefields
method.
Grouping
If your model/table has a grouping field (usually a foreign key): id,
user_id
, title, order_column
and you'd like the above methods to take it into considerations, you can create a buildSortQuery
method at your model:
public function buildSortQuery() { return static::query()->where('user_id', $this->user_id); }
This will restrict the calculations to fields value of the model instance.
Example
use MichielKempen\NovaOrderField\Orderable; use MichielKempen\NovaOrderField\OrderField; class Page extends Resource { use Orderable; public static $defaultOrderField = 'order'; public function fields(Request $request) { return [ OrderField::make('Order'), ]; } }