From dd1c717b99a8e4768de440dd113baf41520819b1 Mon Sep 17 00:00:00 2001 From: Eleri Ojavere Date: Wed, 4 Aug 2021 13:57:03 +0300 Subject: [PATCH] Make categories resource sortable --- composer.json | 3 +- ...8_03_000000_add_sort_order_to_category.php | 33 +++++++++++++++++++ src/Models/Category.php | 10 +++++- src/Nova/Category.php | 2 ++ src/Nova/Post.php | 2 +- 5 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 database/migrations/2021_08_03_000000_add_sort_order_to_category.php diff --git a/composer.json b/composer.json index 6e77390..7345011 100755 --- a/composer.json +++ b/composer.json @@ -32,7 +32,8 @@ "optimistdigital/nova-locale-field": "^2.0", "whitecube/nova-flexible-content": "^0.2.4", "optimistdigital/nova-multiselect-field": ">=1.8.1", - "digital-creative/conditional-container": "^1.3" + "digital-creative/conditional-container": "^1.3", + "optimistdigital/nova-sortable": "^2.3.4" }, "autoload": { "psr-4": { diff --git a/database/migrations/2021_08_03_000000_add_sort_order_to_category.php b/database/migrations/2021_08_03_000000_add_sort_order_to_category.php new file mode 100644 index 0000000..c3ed11c --- /dev/null +++ b/database/migrations/2021_08_03_000000_add_sort_order_to_category.php @@ -0,0 +1,33 @@ +integer('sort_order'); + }); + + DB::statement("UPDATE $categoriesTable SET sort_order = id"); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + } +} diff --git a/src/Models/Category.php b/src/Models/Category.php index 00bcaa3..bffafa3 100644 --- a/src/Models/Category.php +++ b/src/Models/Category.php @@ -2,10 +2,18 @@ namespace OptimistDigital\NovaBlog\Models; +use Spatie\EloquentSortable\Sortable; +use Spatie\EloquentSortable\SortableTrait; use Illuminate\Database\Eloquent\Model; -class Category extends Model +class Category extends Model implements Sortable { + use SortableTrait; + + public $sortable = [ + 'order_column_name' => 'sort_order', + 'sort_when_creating' => true, + ]; public function __construct(array $attributes = []) { parent::__construct($attributes); diff --git a/src/Nova/Category.php b/src/Nova/Category.php index b2ca62b..c60c6c4 100644 --- a/src/Nova/Category.php +++ b/src/Nova/Category.php @@ -6,9 +6,11 @@ use Laravel\Nova\Fields\ID; use Laravel\Nova\Fields\Text; use OptimistDigital\NovaBlog\Nova\Fields\Slug; +use OptimistDigital\NovaSortable\Traits\HasSortableRows; class Category extends TemplateResource { + use HasSortableRows; public static $displayInNavigation = false; /** diff --git a/src/Nova/Post.php b/src/Nova/Post.php index 2c646f2..66b28cb 100755 --- a/src/Nova/Post.php +++ b/src/Nova/Post.php @@ -72,7 +72,7 @@ public function fields(Request $request) if (config('nova-blog.include_froala_texteditor_option')) { $postContent->addLayout('Text section in Froala', 'text_froala', [ - Froala::make('Text section in Froala', 'text_content_froala'), + Froala::make('Text section in Froala', 'text_content_froala') ]); }