I am building an app which imports data from various eCommerce platforms, including Shopify. That data can change when an admin updates an order. The app must always have an up-to-date record, as well as audit history to see what changed and when. I'm using the Laravel Auditing package for recording changes.
Ideally, my code should only be responsible for:
Then I would like Eloquent to handle the heavy lifting of:
For example, if the order itself didn't change, but the address did, when I call $order->save()
it should update the address, and save audit history.
One option could be to check if the Address
or OrderItem
exists in the DB before populating the model with data. That's not ideal because:
Address
models would need to know what Order
model the address is for. This too violates SRP.createNewOrder()
and updateExistingOrder()
, to avoid code duplication.When the app sees an order for the first time, it populates models, then INSERTs records for these entities like so:
$order = SalesOrder::firstOrNew([
'store_id' => $orderData['store_id'],
'external_id' => $orderData['external_id']
]);
$order->save();
$order->salesOrderAddresses()->save($billingAddress);
if(@$shippingAddress) {
$order->salesOrderAddresses()->save($shippingAddress);
}
$order->salesOrderItems()->saveMany($items);
I have audit history working for the order itself. That part works fine, but it would be great if I could build up the entire order model first, then call a single save()
make Eloquent intelligently save any changes to the database.
In the SalesOrder
class, I have these relations:
public function salesOrderAddresses() {
return $this->hasMany(SalesOrderAddress::class,'order_id');
}
public function salesOrderItems() {
return $this->hasMany(SalesOrderItem::class,'order_id');
}
The hierarchy is like this:
SalesOrder = [
Addresses [
Address (billing),
Address (shipping)
],
Items = [
[
sku: abc,
qty: 123
],
[
sku: xyz,
qty: 321
]
]
]
Anyway, I'm probably approaching this entirely the wrong way. Hopefully some seasoned Eloquent/Laravel dev out there can suggest a better way of handling this.