Repeater inside a relationship manager action

I am trying to implement a repeater inside of a modal form within a relationship manager for products. My relationship structure is as follows: Order -> Product -> Attachment -> RevenueLine. I am trying to add the action to the ProductRelationManager so I can have a link inside the table of products in the order to add new attachments instead of opening the product edit page. The repeater is nicely shown, and I can create new database records through this repeater However after I clicked save and reopen the dialog it does not show the data. I can see in the docs that I need to fill the form but how can this be done for the repeater field? Also when I add a new attachment after I created the first few, it tries to delete all the attachments and then reinsert. This is not possible for my structure because every attachment has a relationship with revenueLines so it returns a foreign key constraint error
No description
No description
Solution:
I found it, you indeed need to fill the form with the relationship data manually: ```php Action::make('attachments') ->icon('heroicon-o-link') ->fillForm(fn (Product $record): array => [...
Jump to solution
10 Replies
gladjanus43
gladjanus436mo ago
This is currently my action with the form:
Action::make('attachments')
->icon('heroicon-o-link')
->form([
Repeater::make('attachments')
->relationship()
->schema([
Select::make('addition_id')
->relationship(
name: 'addition',
titleAttribute: 'name',
modifyQueryUsing: fn ($query) => $query->whereIn('addition_type', ['PRODUCT'])
)
->required()
->reactive()
->afterStateUpdated(function (callable $set, $get) {
$addition = Addition::find($get('addition_id'));
if ($addition) {
$set('price', $addition->price);
} else {
$set('price', null);
}
}),
TextInput::make('price')
->numeric('decimal')
->required()
->hintIcon('heroicon-o-question-mark-circle', tooltip: 'Price per unit or per meter'),
TextInput::make('amount')
->type('number')
->required()
->hintIcon('heroicon-o-question-mark-circle', tooltip: 'Amount of units or supports'),
TextInput::make('length')
->label(__('Length (cm)'))
->hintIcon('heroicon-o-question-mark-circle', tooltip: 'Lenght of the addition in meters')
->required(fn ($get) => Addition::find($get('addition_id'))->unit === 'M')
->visible(function (callable $get) {
$addition = Addition::find($get('addition_id'));
if ($addition && $addition->unit === 'M') {
return true;
} else {
return false;
}
})
])

])
Action::make('attachments')
->icon('heroicon-o-link')
->form([
Repeater::make('attachments')
->relationship()
->schema([
Select::make('addition_id')
->relationship(
name: 'addition',
titleAttribute: 'name',
modifyQueryUsing: fn ($query) => $query->whereIn('addition_type', ['PRODUCT'])
)
->required()
->reactive()
->afterStateUpdated(function (callable $set, $get) {
$addition = Addition::find($get('addition_id'));
if ($addition) {
$set('price', $addition->price);
} else {
$set('price', null);
}
}),
TextInput::make('price')
->numeric('decimal')
->required()
->hintIcon('heroicon-o-question-mark-circle', tooltip: 'Price per unit or per meter'),
TextInput::make('amount')
->type('number')
->required()
->hintIcon('heroicon-o-question-mark-circle', tooltip: 'Amount of units or supports'),
TextInput::make('length')
->label(__('Length (cm)'))
->hintIcon('heroicon-o-question-mark-circle', tooltip: 'Lenght of the addition in meters')
->required(fn ($get) => Addition::find($get('addition_id'))->unit === 'M')
->visible(function (callable $get) {
$addition = Addition::find($get('addition_id'));
if ($addition && $addition->unit === 'M') {
return true;
} else {
return false;
}
})
])

])
Debishree Tripathy
Hey, sorry to bother you, I have not worked yet with repeaters. But I want to know how you are adding the logic for total amount? I mean, I have a order in where I need to show the products and the price of that product, also it can be multiple products too.
gladjanus43
gladjanus436mo ago
What exactly is your scenario? You have an order with multiple products and want to show the price of all products in the order?
Debishree Tripathy
Yes exactly, suppose I am choosing a product then the price should show in the total amount which is inside order, similarly if i am choosing multiple products so the amount of all products means whatever i am choosing it should directly show the total amount of that multiple products which is inside the order. Got it I guess? The products I will choose it should show the amount accordingly if its single then it should show only amount of that single product similarly if its multiple then the amount should calculate and show the total amount of multiple products. Got it?
gladjanus43
gladjanus436mo ago
If you are inside a table of your order and have set up a relation between order and its products, then you can do something like this: TextColumn::make('total_price')->getStateUsing(fn($record) => $record->products->sum('price')) This will then create a column in your table with the sum of all products price
Debishree Tripathy
Please check this and let me know.
gladjanus43
gladjanus436mo ago
Can you create a sepparete help question? I will respond in there! 🙂
Debishree Tripathy
Sure. Okay forget about this large file, sorry about it. But I really need a help with this actually. So I added this TextColumn::make('total_price')->getStateUsing(fn($record) => $record->products->sum('price')) Anything I need to change in migration? $table->decimal('total_amount', 10, 2)->default(0.00); basically I have this in my migration file.
Solution
gladjanus43
gladjanus436mo ago
I found it, you indeed need to fill the form with the relationship data manually:
Action::make('attachments')
->icon('heroicon-o-link')
->fillForm(fn (Product $record): array => [
'attachments' => $record->attachments
])
->form([
Repeater::make('attachments')
->relationship('attachments')
->schema([
FORM ELEMENTS
])
])
Action::make('attachments')
->icon('heroicon-o-link')
->fillForm(fn (Product $record): array => [
'attachments' => $record->attachments
])
->form([
Repeater::make('attachments')
->relationship('attachments')
->schema([
FORM ELEMENTS
])
])
TrollMan
TrollMan6mo ago
3,