Ad

Laravel: Is Having 2 Pivot Tables Between 2 Models Possible?

In my project I have 2 models:

  • Transactions

    • id
    • action_id
    • user_id
  • Items

    • id
    • name
    • description

Actions: 'deliver', 'order', 'used', 'spoiled'

Pivot Tables: : item_order, item_transaction

Here's what I want to achieve

  • Have one Transaction table to record actions done to items
  • If action is 'order' then it will save the request form to item_order pivot table,
  • Otherwise, if action is 'deliver', 'used' or 'spoiled' it will record to 'item_transaction' pivot table.

I know I could make another table called 'order' but it will defeat the purpose of having only one 'transaction' table for easier monitoring of actions done to items.

Is it possible to have two different pivot tables between two models? How will I create the relationship?

Here's the link to an image of my Database Structure

Ad

Answer

As proposed in comment, your item_transaction is tied to actions over transactions row. Since it can be only one of action (transactions 1:m actions) solution could be to set order_qty field as nullable field into item_transaction table and completely removing item_order table. Having it null in there it can also be flag wether action is order wether something else.

Ad
source: stackoverflow.com
Ad