ofcold / nova-sortable
A Laravel Nova tool.
Installs: 224 296
Dependents: 0
Suggesters: 0
Security: 0
Stars: 33
Watchers: 1
Forks: 10
Open Issues: 6
Language:Vue
Requires
- php: >=7.1.0
README
Adds sorting functionality to Laravel Nova's index resource
Installing
composer require ofcold/nova-sortable
Using
- Add a sort field to your database migrations file.
Example
$table->unsignedInteger('sort_order')->nullable();
- Trait entry
use Ofcold\NovaSortable\SortableTrait; class Entry extends Model { use SortableTrait; }
- Specify whether the resource needs to be sorted.
class Example extends Resource { /** * Build an "index" query for the given resource. * * @param \Laravel\Nova\Http\Requests\NovaRequest $request * @param \Illuminate\Database\Eloquent\Builder $query * * @return \Illuminate\Database\Eloquent\Builder */ public static function indexQuery(NovaRequest $request, $query) { $query->when(empty($request->get('orderBy')), function ($q) { $q->getQuery()->orders = []; return $q->orderBy(static::$model::orderColumnName()); }); return $query; } /** * Prepare the resource for JSON serialization. * * @param \Laravel\Nova\Http\Requests\NovaRequest $request * @param \Illuminate\Support\Collection $fields * * @return array */ public function serializeForIndex(NovaRequest $request, $fields = null) { return array_merge(parent::serializeForIndex($request, $fields), [ 'sortable' => true ]); } }
Change sort field name
You only need to change the method 'orderColumnName' in your entry.
/* * Determine the column name of the order column. */ public static function orderColumnName(): string { return 'your sort order column name'; }