Skip to content

Commit

Permalink
Try to fix attribute addition
Browse files Browse the repository at this point in the history
  • Loading branch information
Cocoa committed Mar 25, 2024
1 parent 62f19af commit 5513ce5
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/Models/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ public function getValueAttribute()
*/
public function items()
{
return $this->belongsToMany(Item::class);
return $this->belongsToMany(Item::class)->withPivot('value');
}
}
5 changes: 5 additions & 0 deletions app/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,9 @@ public function categories()
{
return $this->belongsToMany('App\Models\Category');
}

public function attributes()
{
return $this->belongsToMany(Attribute::class)->withPivot('value');
}
}
10 changes: 8 additions & 2 deletions app/Nova/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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(),
Expand Down
10 changes: 5 additions & 5 deletions app/Nova/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 5513ce5

Please sign in to comment.