mardi 28 août 2018

Laravel get recently created related models without using with or load

I have a specific issue where I am creating a related model as such:

$detail = new \App\Detail
$detail->item = "Soda Pop"
$detail->quantity += 1
$order->details()->save($detail)

This code is within a foreach statement, and I end up creating a detail for each item purchased. But I have a specific requirement that if an item already exists in the details list, then add +1 to it.

But on the save the relationship, I don't have access to the objects and it forces me to reload from the database. So either I have to perform a $order->load('details') to make the code work.

Is there any way I can access the recently created object and just check from memory if it was inserted?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire