There is no restful easy way to do this indeed because you need to interact with both models. I would probably create a separate controller just for this specific action. Like AddProductOrderController. Your route can then look like this /order/{order:code}/products/add, just like you have now.
Jun 28, 2020
1
Level 3
Create route to attach a Product model to Order model
Background Information
I'm building an invoicing system...
I created:
- Product Model with a restful ProductController
- Order Model with a restful OrderController
Relationships:
- Product belongToMany Order
- Order belongToMany Product
My Goal:
From my orders/show.blade.php I want to be able to add more products to that specific order.
What I've Tried:
I created a form in the orders/show.blade.php view that hits this route below
Route::patch('orders/{order:code}/products/add', 'OrderController@addProduct')->name('orders.products.add');
My Problem / Doubts:
Does this violate the Laravel Restful controller? Should I create another controller called OrderProductController? Is there a better way of accomplishing this?
Level 88
Please or to participate in this conversation.