Skip to content

Commit

Permalink
Get order column name from the model or config instead of resource cu…
Browse files Browse the repository at this point in the history
…stom static property
  • Loading branch information
Naxon committed Jul 30, 2021
1 parent 8a2d6d9 commit 95c7a19
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -46,8 +45,6 @@ class Page extends Resource
{
use SortsIndexEntries;

public static $defaultSortField = 'sort_order';

/**
* Get the fields displayed by the resource.
*
Expand Down Expand Up @@ -78,4 +75,4 @@ If you discover any security related issues, please email [email protected] inste

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
23 changes: 16 additions & 7 deletions src/Concerns/SortsIndexEntries.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

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];
}
}

0 comments on commit 95c7a19

Please sign in to comment.