Fill repeater on createView

I'm facing an issue while implementing an invoice creation . I'm using the Repeater component to display order items (commandeItems) Here's a summary of what I have set up: I have a Commande model with a hasMany relationship to CommandeItem. I have an Invoice model with a hasMany relationship to CommandeItem I'm using CreateInvoice, which extends Filament's CreateRecord to create a new invoice. In CreateInvoice, I fetch a specific order with its associated articles (commandeItems) and attempt to pre-fill them in the form using mapCommandeItemsForForm(). Despite this, the Repeater for commandeItems stays empty when I open the form.
class CreateInvoice extends CreateRecord
{
protected static string $resource = InvoiceResource::class;

public $commande;

public function mount(): void
{
parent::mount();

$commandeId = request()->query('commande');
if ($commandeId) {
$this->commande = Commande::with('fournisseur', 'commandeItems')
->find($commandeId);
$this->fillFormWithCommandeData();
} else {
$this->commande = null;
}
}
class CreateInvoice extends CreateRecord
{
protected static string $resource = InvoiceResource::class;

public $commande;

public function mount(): void
{
parent::mount();

$commandeId = request()->query('commande');
if ($commandeId) {
$this->commande = Commande::with('fournisseur', 'commandeItems')
->find($commandeId);
$this->fillFormWithCommandeData();
} else {
$this->commande = null;
}
}
then i fill my form
protected function fillFormWithCommandeData(): void
{
if ($this->commande) {
$this->form->fill([
'commande_id' => $this->commande->id,
'fournisseur_id' => $this->commande->fournisseur->id,
'commandeItems' => $this->mapCommandeItemsForForm(),// return empty
]);
}
}
protected function fillFormWithCommandeData(): void
{
if ($this->commande) {
$this->form->fill([
'commande_id' => $this->commande->id,
'fournisseur_id' => $this->commande->fournisseur->id,
'commandeItems' => $this->mapCommandeItemsForForm(),// return empty
]);
}
}
4 Replies
karim charfaoui
karim charfaouiOP5mo ago
an dthis my mapCommandeItemsForm()
protected function mapCommandeItemsForForm(): array
{
if (!$this->commande || !$this->commande->commandeItems) {
return [];
}

return $this->commande->commandeItems->map(function ($commandeItem) {
return [
'article_id' => $commandeItem->article_id,
'quantity' => $commandeItem->quantity,
'unit_price_ht' => $commandeItem->unit_price_ht,
'unit_price_ttc' => $commandeItem->unit_price_ttc,
'total_price_ht' => $commandeItem->total_price_ht,
'total_price_ttc' => $commandeItem->total_price_ttc,
'tva' => $commandeItem->tva,
];
})->toArray();
}
protected function mapCommandeItemsForForm(): array
{
if (!$this->commande || !$this->commande->commandeItems) {
return [];
}

return $this->commande->commandeItems->map(function ($commandeItem) {
return [
'article_id' => $commandeItem->article_id,
'quantity' => $commandeItem->quantity,
'unit_price_ht' => $commandeItem->unit_price_ht,
'unit_price_ttc' => $commandeItem->unit_price_ttc,
'total_price_ht' => $commandeItem->total_price_ht,
'total_price_ttc' => $commandeItem->total_price_ttc,
'tva' => $commandeItem->tva,
];
})->toArray();
}
thenks for your help an this my repeater on InvoiceResource
Repeater::make('commandeItems')
->label('Articles')
->relationship('commandeItems')
->schema([
Forms\Components\Select::make('article_id')
->options(Article::all()->pluck('name', 'id'))
->searchable(),
Forms\Components\TextInput::make('quantity')
->required()
->numeric(),
Forms\Components\TextInput::make('unit_price_ht')
->required()
->numeric(),
Forms\Components\Select::make('tva')
->required()
->enum(TvaEnum::class)
->options(TvaEnum::class),
Forms\Components\Hidden::make('unit_price_ttc')
->required(),
Forms\Components\TextInput::make('total_price_ht')
->readOnly()
->numeric(),
Forms\Components\TextInput::make('total_price_ttc')
->readOnly()
->required()
->numeric(),
])
Repeater::make('commandeItems')
->label('Articles')
->relationship('commandeItems')
->schema([
Forms\Components\Select::make('article_id')
->options(Article::all()->pluck('name', 'id'))
->searchable(),
Forms\Components\TextInput::make('quantity')
->required()
->numeric(),
Forms\Components\TextInput::make('unit_price_ht')
->required()
->numeric(),
Forms\Components\Select::make('tva')
->required()
->enum(TvaEnum::class)
->options(TvaEnum::class),
Forms\Components\Hidden::make('unit_price_ttc')
->required(),
Forms\Components\TextInput::make('total_price_ht')
->readOnly()
->numeric(),
Forms\Components\TextInput::make('total_price_ttc')
->readOnly()
->required()
->numeric(),
])
plz any help commande mean order
krekas
krekas5mo ago
first thing don't mix two languages, it's too hard to read. use english. as for the problem do a dd($this->commande->commandeItems) in your mapCommandeItemsForForm method to check if get anything also, when using livewire wouldn't rely on the request to get some variable
karim charfaoui
karim charfaouiOP5mo ago
@krekas dd($this->mapCommandeItemsForForm());
No description
krekas
krekas5mo ago
so it's already an array? then map should throw error as it's not a collection
Want results from more Discord servers?
Add your server