F
Filament12mo ago
Damien

Repeater fields are missing on view/edit

I have a repeater field that I add multiple values to but when I then view the record or try to edit it, only the first one in shown. Here is the code for my repeater:
$products = Wizard\Step::make('Stock Products')
->schema([
Repeater::make('Stock Products')->schema([
Select::make('stock_product_id')
->options(
StockProduct::class::all()->pluck('name', 'id'),
)
->searchable()
->hiddenLabel(true)
->required(),
])
->relationship('stockProducts')
->itemLabel('Select Stock Product')
->addActionLabel('Add To Group')
->columnSpan(3)
->collapsible(),
]);
$products = Wizard\Step::make('Stock Products')
->schema([
Repeater::make('Stock Products')->schema([
Select::make('stock_product_id')
->options(
StockProduct::class::all()->pluck('name', 'id'),
)
->searchable()
->hiddenLabel(true)
->required(),
])
->relationship('stockProducts')
->itemLabel('Select Stock Product')
->addActionLabel('Add To Group')
->columnSpan(3)
->collapsible(),
]);
This is for a simple resource so I don't know if there is something I should be doing to get all of the values I created the record with? In terms of my database, this Wizard Step is for a group of products and I am looking to add many products. I have a group products table, stock products table and a group stock products table. Not entirely sure what is missing at this point of where I should look in the docs.
27 Replies
Damien
DamienOP12mo ago
(apologies for the formatting in advance) When I view a group of products, the 2nd step of the wizard should show all of the stock items. At the moment, it only displays one.
toeknee
toeknee12mo ago
what is the relationship return
Damien
DamienOP12mo ago
I don't know what you mean by relationship return
toeknee
toeknee12mo ago
You have a relationshup called stockProducts
Damien
DamienOP12mo ago
ah sorry yes
toeknee
toeknee12mo ago
the function stockProducts has a return
Damien
DamienOP12mo ago
one second
public function stockProducts(): HasMany
{
return $this->hasMany(BomStockProduct::class);
}
public function stockProducts(): HasMany
{
return $this->hasMany(BomStockProduct::class);
}
it should return them all?
toeknee
toeknee12mo ago
It should, can you run: ->formatStateUsing(fn($state) => dd($state)) to the repeater, What does it show
Damien
DamienOP12mo ago
Let me have a look I assume you mean on the wizard step for this? If so, dd does not fire. If there is somewhere else I am supposed to add the repeater, this is likely why it isn't working as I thought it was only necessary on creation.
toeknee
toeknee12mo ago
I mean on:
Repeater::make('Stock Products')->schema([
Select::make('stock_product_id')
->options(
StockProduct::class::all()->pluck('name', 'id'),
)
->searchable()
->hiddenLabel(true)
->required(),
])
->formatStateUsing(fn($state) => dd($state))
->relationship('stockProducts')
->itemLabel('Select Stock Product')
->addActionLabel('Add To Group')
->columnSpan(3)
->collapsible(),
Repeater::make('Stock Products')->schema([
Select::make('stock_product_id')
->options(
StockProduct::class::all()->pluck('name', 'id'),
)
->searchable()
->hiddenLabel(true)
->required(),
])
->formatStateUsing(fn($state) => dd($state))
->relationship('stockProducts')
->itemLabel('Select Stock Product')
->addActionLabel('Add To Group')
->columnSpan(3)
->collapsible(),
Damien
DamienOP12mo ago
yeah that is what I did.
toeknee
toeknee12mo ago
That should die on that repeater as it allows formatting the state for population. But it is possible it's not used on the repeater but it is defined in the namespace
Damien
DamienOP12mo ago
I was trying to view a record, let me try and create one
toeknee
toeknee12mo ago
What if you do:
Repeater::make('Stock Products')->schema([
Select::make('stock_product_id')
->options(
StockProduct::class::all()->pluck('name', 'id'),
)
->searchable()
->hiddenLabel(true)
->required(),
])
->afterStateUpdated(fn($state) => dd($state))
->relationship('stockProducts')
->itemLabel('Select Stock Product')
->addActionLabel('Add To Group')
->columnSpan(3)
->collapsible(),
Repeater::make('Stock Products')->schema([
Select::make('stock_product_id')
->options(
StockProduct::class::all()->pluck('name', 'id'),
)
->searchable()
->hiddenLabel(true)
->required(),
])
->afterStateUpdated(fn($state) => dd($state))
->relationship('stockProducts')
->itemLabel('Select Stock Product')
->addActionLabel('Add To Group')
->columnSpan(3)
->collapsible(),
Essentially you need to dump the relationship on the repeater to see what it has returned
Damien
DamienOP12mo ago
interesting, dd does not fire on creation either.. let me try your 2nd example. that fires when I click to add a new product or try to edit an exsiting one:
array:2 [// app/Filament/Resources/BomProductResource.php:76
"record-" => array:2 [
"bom_product_id" => 1
"stock_product_id" => 1
]
"5db5a1c8-abec-4e96-8418-f917565225ea" => array:1 [
"stock_product_id" => null
]
]
array:2 [// app/Filament/Resources/BomProductResource.php:76
"record-" => array:2 [
"bom_product_id" => 1
"stock_product_id" => 1
]
"5db5a1c8-abec-4e96-8418-f917565225ea" => array:1 [
"stock_product_id" => null
]
]
This group should have 2 products however
toeknee
toeknee12mo ago
That is strange, I am unsure why you are getting that, hasMany should be good
Damien
DamienOP12mo ago
I will try and see if a non-simple resource presents the same problem and if not, maybe move to that.
LeandroFerreira
LeandroFerreira12mo ago
what about the bom_stock_products table? What was stored?
Damien
DamienOP12mo ago
it was storing it correctly, 2 rows, both with the same group id and different stock ids
LeandroFerreira
LeandroFerreira12mo ago
can you show it please?
Damien
DamienOP12mo ago
The DB rows?
LeandroFerreira
LeandroFerreira12mo ago
yep
Damien
DamienOP12mo ago
sure, one moment as I done a fresh migration, working on a couple of bits this morning. So for context, I just created 2 stock products, then a group product (bom) with the 2 stock products. I grabbed a screen shot of them all for you which I will now upload.
Damien
DamienOP12mo ago
Hopefully the order persists
No description
No description
No description
No description
No description
Damien
DamienOP12mo ago
As you can see it seems like it is all working as expected with the exception of the view. I did just notice however that the view returns only the last record.
LeandroFerreira
LeandroFerreira12mo ago
add an id pk in the bom_stock_products table
Damien
DamienOP12mo ago
I will give that a try. yes that worked, what a silly mistake 🤦‍♂️ - thought it would be fine as it was. Thank you Leandro!
Want results from more Discord servers?
Add your server