Many-to-many pivot table with same table using Orchid CRUD resource #2302
Unanswered
LemonPie22
asked this question in
Q&A
Replies: 1 comment 5 replies
-
Something like this would look like saving the relationship: /**
* Action to create and update the model
*
* @param ResourceRequest $request
* @param Model $model
*/
public function save(ResourceRequest $request, Model $model): void
{
$model->fill($request->all())->save();
$model->products()->save($request->get('product_id'));
} |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to implement a hierarchical relationship between products, using Orchid CRUD, where any product can have multiple sub-products (known as components) as well as multiple parent products.
For example, a car has an engine which has an oil cooler which has a bolt. The same bolt, which is a component of the oil cooler, could be used on other parts too.
To achieve this I have a main
products
table and a pivot table calledproduct_component
.The
products
table hasid
andname
columns.The
product_component
table hasproduct_id
andcomponent_id
columns, both of which have foreign key relationships to theproducts
table.I'm struggling to show product components (the first step; I can do parents after) using an Orchid CRUD resource with a
Relation
field.My
Product
model contains the following relationships:And my
ProductResource
contains the followingFieldsRelation
:This allows me to add products in the UI:
But as soon as I try to save I get this error:
I understand that I shouldn't be trying to save into the main Products table but can't work out how to save to the pivot table instead.
I've tried lots of things, and have read articles on here that touch on this topic, but can't seem to make any progress so I'd really appreciate some help.
Beta Was this translation helpful? Give feedback.
All reactions