yonka's avatar
Level 2

Update All related Models at Once

I want to update all related I have 4 Models below SalesInvoice SoldInventory SoldInventoryItems InventoryInStore

Relationships

SalesInvoice has One SoldInventory

class SalesInvoice extends Model
{
    use HasFactory;

    protected $fillable = ['sold_inventory_id','customer_id','invoice_no','reference_no','issue_date','sub_total','vat','discount','total_amount','balance','invoice_status'];

    public function soldInventory()
    {
        return $this->belongsTo(SoldInventory::class);
    }

    public function customer() :BelongsTo
    {
        return $this->belongsTo(Customer::class);
    }
}

SoldInventory hasMany SoldInventoryItems

class SoldInventory extends Model
{
    use HasFactory;

    protected $fillable = ['customer_id','store_id','date_of_sale','tracking_no','reference','description','user_id'];

    public function soldInventoryItems()
    {
        return $this->hasMany(SoldInventoryItems::class);
    }
  
    public function salesInvoice()
    {
        return $this->hasOne(SalesInvoice::class);
    }
}

InventoryInStore has Many SoldInventoryItems

class InventoryInStore extends Model
{
    use HasFactory, CreatedUpdatedBy, softDeletes;

    protected $fillable = ['inventory_item_id','quantity','store_id'];

    public function store()
    {
        return $this->belongsTo(Store::class);
    }

    public function inventoryItem()
    {
        return $this->belongsTo(InventoryItem::class);
    }

    public function soldInventoryItems()
    {
        return $this->hasMany(SoldInventoryItems::class);
    }

}

SoldInventoryItem

class SoldInventoryItems extends Model
{
    use HasFactory, CreatedUpdatedBy, softDeletes;

    protected $fillable = ['inventory_in_store_id','quantity','unit_price','sub_total'];


    public function soldInventory()
    {
        return $this->belongsTo(SoldInventory::class);
    }

    public function inventoryInStore() : BelongsTo
    {
        return $this->belongsTo(InventoryInStore::class);
    }

}

So I want to be able to delete SalesInvoice, Once Deleted SalesInvoice, I want to be able to delete SoldInventory, In order to delete SoldInventory, SoldInventoryItems Should be deleted Also. So to delete SoldInventoryItems, Quantity should be added to InventoryInStore Qty because when I was saving SoldInventoryItems, The quantity of SoldInventoryItems subtracted from soldInventory Quantity, So when deleting, SoldInventoryItems Quantity Should be incremented to InventoryInStore Quantity.

1 like
1 reply
LaryAI's avatar
Level 58

To achieve the desired functionality of cascading deletions and updating quantities, you can use Eloquent model events and transactions to ensure data integrity. Here's a step-by-step solution:

  1. Delete the SalesInvoice: When you delete a SalesInvoice, you should also delete the related SoldInventory and its SoldInventoryItems.

  2. Update InventoryInStore Quantities: Before deleting each SoldInventoryItem, increment the quantity in the related InventoryInStore.

  3. Use Transactions: Wrap the operations in a database transaction to ensure that all operations succeed or fail together.

Here's how you can implement this:

Explanation:

  • Transaction: The DB::transaction() method ensures that all database operations within the closure are executed as a single transaction. If any operation fails, the transaction is rolled back.

  • Increment Quantity: Before deleting each SoldInventoryItem, the quantity of the related InventoryInStore is incremented by the quantity of the SoldInventoryItem.

  • Cascading Deletions: After updating the quantities, the SoldInventoryItems are deleted, followed by the SoldInventory, and finally the SalesInvoice.

This approach ensures that your data remains consistent and that all related models are updated and deleted as required.

Please or to participate in this conversation.