From 5513ce5bc6d4335a2a52b684741f0eb3186ebf03 Mon Sep 17 00:00:00 2001 From: Cocoa Date: Mon, 25 Mar 2024 18:22:17 -0400 Subject: [PATCH] Try to fix attribute addition --- app/Models/Attribute.php | 2 +- app/Models/Item.php | 5 +++++ app/Nova/Attribute.php | 10 ++++++++-- app/Nova/Item.php | 10 +++++----- composer.json | 4 ++-- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/app/Models/Attribute.php b/app/Models/Attribute.php index f9ed3e71..01c63485 100644 --- a/app/Models/Attribute.php +++ b/app/Models/Attribute.php @@ -75,6 +75,6 @@ public function getValueAttribute() */ public function items() { - return $this->belongsToMany(Item::class); + return $this->belongsToMany(Item::class)->withPivot('value'); } } diff --git a/app/Models/Item.php b/app/Models/Item.php index 2449e184..5824f4cc 100644 --- a/app/Models/Item.php +++ b/app/Models/Item.php @@ -251,4 +251,9 @@ public function categories() { return $this->belongsToMany('App\Models\Category'); } + + public function attributes() + { + return $this->belongsToMany(Attribute::class)->withPivot('value'); + } } diff --git a/app/Nova/Attribute.php b/app/Nova/Attribute.php index 588b1151..309892d3 100644 --- a/app/Nova/Attribute.php +++ b/app/Nova/Attribute.php @@ -8,6 +8,7 @@ use Laravel\Nova\Fields\Text; use Laravel\Nova\Http\Requests\NovaRequest; use YesWeDev\Nova\Translatable\Translatable; +use Laravel\Nova\Fields\BelongsToMany; class Attribute extends TranslatableResource { @@ -56,8 +57,13 @@ public function fields(Request $request) ->sortable() ->rules('required', 'min:2', 'max:255'), - Text::make('Value') - ->readonly(), + + BelongsToMany::make('Items', 'items', Item::class) + ->fields(function ($request, $relatedModel) { + return [ + Text::make('Value')->readonly() + ]; + })->readonly(), DateTime::make('Created', 'created_at')->onlyOnDetail(), DateTime::make('Updated', 'updated_at')->onlyOnDetail(), diff --git a/app/Nova/Item.php b/app/Nova/Item.php index badbedd4..3dd31b26 100644 --- a/app/Nova/Item.php +++ b/app/Nova/Item.php @@ -158,24 +158,24 @@ public function fields(Request $request) new Panel('Tags and Features', [ AttachMany::make('Features', 'features', Feature::class), AttachMany::make('Tags', 'tags', Tag::class), - AttachMany::make('Colors', 'colors', Color::class), + AttachMany::make('Colors', 'colors', Color::class) ]), // This panel is only shown on the view and edit page new Panel('Tags, Features and Colors', [ BelongsToMany::make('Item Features', 'features', Feature::class)->display('name'), BelongsToMany::make('Item Tags', 'tags', Tag::class)->searchable(), - BelongsToMany::make('Item Colors', 'colors', Color::class)->display('name'), + BelongsToMany::make('Item Colors', 'colors', Color::class)->display('name') ]), new Panel('Attributes', [ // Need to make a custom attributes panel here to allow it on item creation. BelongsToMany::make('Attributes', 'attributes', Attribute::class) - ->fields(function () { + ->fields(function ($request, $relatedModel) { return [ - Text::make('Value')->nullable(), + Text::make('Value')->nullable()->showOnUpdating()->showOnDetail()->showOnCreating() ]; - }), + })->showOnCreating(), ]), Badge::make('Status', function () { diff --git a/composer.json b/composer.json index 8cbc7629..f5c38d6a 100644 --- a/composer.json +++ b/composer.json @@ -13,9 +13,9 @@ "ext-json": "*", "ext-pdo": "*", "astrotomic/laravel-translatable": "^11.9", - "dillingham/nova-attach-many": "^1.0", + "dillingham/nova-attach-many": "^1.3", "laravel/framework": "^9.19.0", - "laravel/nova": "^3.0", + "laravel/nova": "^3.32", "laravel/passport": "^10.0", "laravel/tinker": "^2.0", "laravel/ui": "^3.0",