Repeatable has many relationship. How do I set this up? #1222
Replies: 1 comment
-
To set up a repeatable field for a "has many" relationship like invoice lines in Backpack, you should use the Here's how you can set it up: CRUD::field([
'name' => 'lines',
'label' => 'Invoice Items',
'type' => 'relationship',
'subfields' => [
[
'name' => 'service',
'type' => 'text',
'label' => 'Service',
'wrapper' => ['class' => 'form-group col-md-6'],
],
[
'name' => 'price',
'type' => 'number',
'label' => 'Price',
'attributes' => ["step" => "any"],
'wrapper' => ['class' => 'form-group col-md-6'],
],
],
'new_item_label' => 'Add Invoice Item',
]); This setup will allow you to add, edit, and delete invoice lines directly within the invoice form. The Here are some key points to note:
Make sure you have properly set up the relationship in your Invoice model: public function lines()
{
return $this->hasMany(InvoiceLine::class);
} And in your InvoiceLine model: public function invoice()
{
return $this->belongsTo(Invoice::class);
} This setup will automatically handle saving the related invoice lines when you save the invoice, and it will also display the existing lines in the update operation. Source: Backpack Docs - Relationship Field Remember that you cannot use a Relevant sources: Github Issue (open): [Bug] Upload subfield contains wrong url in repeteable relationship field |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
My client wants to be able to add many "items" to an entity.
Think about an invoice, and invoice lines. The invoice has many lines.
Invoice:
Invoice Lines:
I have a repeatable field that lets you add the service text ("Web Services" for example) and the price, and then you can add another line with the same fields.
I'm trying to get it to save these lines on the invoice model, as well as display them in the update operation.
My invoice model has a hasMany relationship with the lines.
Here is how I'm trying to set it up:
Beta Was this translation helpful? Give feedback.
All reactions