Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jginorio's avatar

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?

0 likes
1 reply
bobbybouwmann's avatar
Level 88

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.

Please or to participate in this conversation.