ofcold/nova-sortable

This package is abandoned and no longer maintained. No replacement package was suggested.

A Laravel Nova tool.

Installs: 216 671

Dependents: 0

Suggesters: 0

Security: 0

Stars: 33

Watchers: 1

Forks: 10

Open Issues: 6

Language:Vue

1.0.6 2019-10-15 15:24 UTC

This package is auto-updated.

Last update: 2020-06-27 04:48:58 UTC


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

Demos

demos.gif?sanitize=true