#Repeater belongsToMany pivot - empty

7 messages · Page 1 of 1 (latest)

proud jewel
#

I've tried multiple ways to find a solutions, and I think the 'quantity' should be automatically resolved? Anything to help me get through this issue, since its been couple of hours and still cant find a proper solution.

proud jewel
#

@inland elk

inland elk
#

#✅┊rules, no tagging please, this is a free forum where someone can help you if they wish

#

last message before I leave this thread again: read the docs for repeaters with BelongsToMany relationships, there are special instructions that you need to follow to set up a pivot relationship

proud jewel
#

Ah sorry. I believe i've checked everything but cant find a proper solution.

icy granite
#

You need to set the field to be a relationship

Forms\Components\Repeater::make('products')
    ->relationship('products')
    ->deleteAction(
        fn (Forms\Components\Actions\Action $action) => $action->requiresConfirmation(),
    )
    ->schema([
        Forms\Components\TextInput::make('quantity')
            ->required()
            ->numeric()
            ->rules(['required', 'numeric'])
            ->default(1)
            ->live(onBlur: true)
            ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, ?string $state) {
                $productId = $get('product_id');
                $product = Product::find($productId);

                $set('price', Number::format($product->price * $state, 2));
            }),
    ])
    ->columns(3),

public function products(): BelongsToMany
    {
        return $this->belongsToMany(Product::class, 'order_product')
            ->withPivot('quantity')
            ->withTimestamps();
    }

Aslo see:
https://filamentphp.com/docs/3.x/panels/resources/relation-managers#creating-with-pivot-attributes