From 95c7a19ba96159ee868a1717c404ed43bdb7c362 Mon Sep 17 00:00:00 2001 From: Daniel Naxon Date: Fri, 30 Jul 2021 18:23:27 +0300 Subject: [PATCH] Get order column name from the model or config instead of resource custom static property --- README.md | 9 +++------ src/Concerns/SortsIndexEntries.php | 23 ++++++++++++++++------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 4f3fc5a..e289874 100644 --- a/README.md +++ b/README.md @@ -24,9 +24,8 @@ composer require naxon/nova-field-sortable ## Usage 1. Follow the [usage instructions](https://github.com/spatie/eloquent-sortable#usage) on the eloquent-sortable repository to make your model sortable. -2. Use the `Naxon\NovaFieldSortable\Concerns\SortsIndexEntries` trait in your Nova Resource. -3. Add a public static property called `$defaultSortField` to your resource, containing your sorting column (I recomment adding it in your main `app/Nova/Resource.php` file). -4. Add the `Naxon\NovaFieldSortable\Sortable` field to your Nova Resource `fields` method, using a label and your primary key column. +1. Use the `Naxon\NovaFieldSortable\Concerns\SortsIndexEntries` trait in your Nova Resource. +1. Add the `Naxon\NovaFieldSortable\Sortable` field to your Nova Resource `fields` method, using a label and your primary key column. ### Example @@ -46,8 +45,6 @@ class Page extends Resource { use SortsIndexEntries; - public static $defaultSortField = 'sort_order'; - /** * Get the fields displayed by the resource. * @@ -78,4 +75,4 @@ If you discover any security related issues, please email naxond@gmail.com inste ## License -The MIT License (MIT). Please see [License File](LICENSE.md) for more information. \ No newline at end of file +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. diff --git a/src/Concerns/SortsIndexEntries.php b/src/Concerns/SortsIndexEntries.php index 73bb190..8478399 100644 --- a/src/Concerns/SortsIndexEntries.php +++ b/src/Concerns/SortsIndexEntries.php @@ -7,21 +7,30 @@ trait SortsIndexEntries { + private static array $orderColumnCache = []; + /** - * Build an "index" query for the given resource. - * - * @param \Laravel\Nova\Http\Requests\NovaRequest $request - * @param \Illuminate\Database\Eloquent\Builder $query + * @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) + public static function indexQuery(NovaRequest $request, $query): Builder { $query->when(empty($request->get('orderBy')), function (Builder $q) { $q->getQuery()->orders = []; - return $q->orderBy(static::$defaultSortField); + return $q->orderBy(static::getOrderColumn(static::$model)); }); return $query; } -} \ No newline at end of file + + private static function getOrderColumn(string $model): string + { + if (!isset(static::$orderColumnCache[$model])) { + static::$orderColumnCache[$model] = app($model)?->sortable['order_column_name'] ?? config('eloquent-sortable.order_column_name', 'order_column'); + } + + return static::$orderColumnCache[$model]; + } +}