Repeater have problem

Hi I have create depot delivery form.
return $form
->schema([
Tabs::make()
->tabs([

Tabs\Tab::make('General')
->schema([

Forms\Components\Select::make('depot_id')
->label(__('depot_deliveries.create.depot'))
->required()
->columnSpanFull()
->relationship('depot', 'name', function ($query) {
return $query->where('active', true);
}),

Forms\Components\TextInput::make('invoice_number')
->label(__('depot_deliveries.create.invoice_number'))
->required()
->columnSpanFull()
->maxLength(255),

Forms\Components\DateTimePicker::make('delivery_date')
->required(),

]),

Tabs\Tab::make('Products')
->schema([

Forms\Components\Repeater::make('products')
->required()
->schema([

Forms\Components\Select::make('product_id')
->label(__('depot_deliveries.create.product_id'))
->relationship('products', 'name')
->required(),

Forms\Components\TextInput::make('quantity')
->label(__('depot_deliveries.create.product_quantity'))
->numeric()
->required(),

Forms\Components\TextInput::make('price')
->label(__('depot_deliveries.create.product_price'))
->numeric()
->required(),

])
->columns(3)
->collapsible(),

]),

])->columnSpanFull(),
]);
return $form
->schema([
Tabs::make()
->tabs([

Tabs\Tab::make('General')
->schema([

Forms\Components\Select::make('depot_id')
->label(__('depot_deliveries.create.depot'))
->required()
->columnSpanFull()
->relationship('depot', 'name', function ($query) {
return $query->where('active', true);
}),

Forms\Components\TextInput::make('invoice_number')
->label(__('depot_deliveries.create.invoice_number'))
->required()
->columnSpanFull()
->maxLength(255),

Forms\Components\DateTimePicker::make('delivery_date')
->required(),

]),

Tabs\Tab::make('Products')
->schema([

Forms\Components\Repeater::make('products')
->required()
->schema([

Forms\Components\Select::make('product_id')
->label(__('depot_deliveries.create.product_id'))
->relationship('products', 'name')
->required(),

Forms\Components\TextInput::make('quantity')
->label(__('depot_deliveries.create.product_quantity'))
->numeric()
->required(),

Forms\Components\TextInput::make('price')
->label(__('depot_deliveries.create.product_price'))
->numeric()
->required(),

])
->columns(3)
->collapsible(),

]),

])->columnSpanFull(),
]);
After I send form I have error (photo). How I can resolve this? Products is a relationship, I have table delivery_products for this products.
21 Replies
__Dementor
__DementorOP7mo ago
And mutate data
protected function mutateFormDataBeforeCreate(array $data): array {

if(!empty($data['products'])) {
$products = $data['products'];

foreach ($products as $product) {
$dataToInsert = [
'delivery_id' => 1,
'price' => $product['price'],
'quantity' => $product['quantity'],
'product_id' => $product['product_id']
];

DeliveryProduct::query()
->insert($dataToInsert);
}

$data['products'] = null;
}

return $data;
}
protected function mutateFormDataBeforeCreate(array $data): array {

if(!empty($data['products'])) {
$products = $data['products'];

foreach ($products as $product) {
$dataToInsert = [
'delivery_id' => 1,
'price' => $product['price'],
'quantity' => $product['quantity'],
'product_id' => $product['product_id']
];

DeliveryProduct::query()
->insert($dataToInsert);
}

$data['products'] = null;
}

return $data;
}
__Dementor
__DementorOP7mo ago
No description
LeandroFerreira
LeandroFerreira7mo ago
If you are trying to do this https://demo.filamentphp.com/shop/orders/create I recommend check the demo project: https://github.com/filamentphp/demo
GitHub
GitHub - filamentphp/demo: Source code for the demo.filamentphp.com...
Source code for the demo.filamentphp.com website. Contribute to filamentphp/demo development by creating an account on GitHub.
__Dementor
__DementorOP7mo ago
Omg That was too easy Thanks bro ❤️ Whats wrong?
Forms\Components\Repeater::make('Select products')
->relationship('items')
->required()
->live()
->schema([
Forms\Components\Select::make('product_id')
->label(__('depot_deliveries.create.product_id'))
->options(function (Forms\Get $get) {
return Product::query()
->where('depot_id', $get('depot_id'))
->pluck('name', 'id');
})
->distinct()
->disableOptionsWhenSelectedInSiblingRepeaterItems()
->reactive()
->live()
->afterStateUpdated(function ($state, Forms\Set $set) {
$set('system_quantity', Product::query()->find($state)?->quantity ?? 0);
$set('price', Product::query()->find($state)?->price ?? 0);
})
->searchable()
->columnSpanFull()
->required(),


])
->columns(2),
Forms\Components\Repeater::make('Select products')
->relationship('items')
->required()
->live()
->schema([
Forms\Components\Select::make('product_id')
->label(__('depot_deliveries.create.product_id'))
->options(function (Forms\Get $get) {
return Product::query()
->where('depot_id', $get('depot_id'))
->pluck('name', 'id');
})
->distinct()
->disableOptionsWhenSelectedInSiblingRepeaterItems()
->reactive()
->live()
->afterStateUpdated(function ($state, Forms\Set $set) {
$set('system_quantity', Product::query()->find($state)?->quantity ?? 0);
$set('price', Product::query()->find($state)?->price ?? 0);
})
->searchable()
->columnSpanFull()
->required(),


])
->columns(2),
This
->options(function (Forms\Get $get) {
return Product::query()
->where('depot_id', $get('depot_id'))
->pluck('name', 'id');
})
->options(function (Forms\Get $get) {
return Product::query()
->where('depot_id', $get('depot_id'))
->pluck('name', 'id');
})
Doesn't work. Without where() works, with where doesnt work 😦
LeandroFerreira
LeandroFerreira7mo ago
what is the value from depot_id? Is it live?
__Dementor
__DementorOP7mo ago
Tabs\Tab::make('Depot')
->schema([
Forms\Components\Select::make('depot_id')
->options(Depot::query()->pluck('name', 'id'))
->required()
->live()
->reactive(),
]),
Tabs\Tab::make('Depot')
->schema([
Forms\Components\Select::make('depot_id')
->options(Depot::query()->pluck('name', 'id'))
->required()
->live()
->reactive(),
]),
LeandroFerreira
LeandroFerreira7mo ago
->reactive() isn't necessary if you are using live on V3 if you change depot_id, the query should work in the product_id..
__Dementor
__DementorOP7mo ago
I remove ->reactive() and this still not working
__Dementor
__DementorOP7mo ago
No description
LeandroFerreira
LeandroFerreira7mo ago
what is the value from depot_id?
__Dementor
__DementorOP7mo ago
Value is id, label is name
No description
LeandroFerreira
LeandroFerreira7mo ago
use this instead of options
->getSearchResultsUsing(function (string $search, Get $get): array {

return Product::query()
->whereDepotId($get('depot_id'))
->where('name', 'like', "%{$search}%")
->pluck('name', 'id');
})
->getSearchResultsUsing(function (string $search, Get $get): array {

return Product::query()
->whereDepotId($get('depot_id'))
->where('name', 'like', "%{$search}%")
->pluck('name', 'id');
})
__Dementor
__DementorOP7mo ago
I understand that ->whereDepotId() is a scope?
LeandroFerreira
LeandroFerreira7mo ago
short way of ->where('depot_id', $get('depot_id'))
__Dementor
__DementorOP7mo ago
Still not working
LeandroFerreira
LeandroFerreira7mo ago
hum, not sure why Could you share the whole code you are using?
__Dementor
__DementorOP7mo ago
Github or only this resource and models?
LeandroFerreira
LeandroFerreira7mo ago
are you able to share the project on github?
__Dementor
__DementorOP7mo ago
I have it shared, but privately. Is it a problem if you gave me your email address?
LeandroFerreira
LeandroFerreira7mo ago
no problem
LeandroFerreira
LeandroFerreira7mo ago
Sorry, I didn't notice that you are using a repeater. This should be $get('../../depot_id') https://filamentphp.com/docs/3.x/forms/fields/repeater#using-get-to-access-parent-field-values ✌️
Want results from more Discord servers?
Add your server