I'm working through understanding relationships in laravel. I have a relationship set up between two tables: reservation and receipt. receipt belongs to reservation and reservation has many receipts. I'm fine with the basic usage and can associate receipts to the reservation. However I can;t work out how to carry out an update process. From a form I have a cart type application with a cart storing receipts to process against the reservation the cart includes existing receipts, new receipts added by the user and the user may have removed a receipt. There are a couple of things I'm not sure about:
If I simply want to update details of the receipt from the reservation record how can I do this?
I have tried:
$reservation->invoice->update();
and:
$reservation->invoice->save();
after updating the properties but these methods come up with an error: method not found. Do I assume then I should simply find the record separately from the relationship and update as normal?
Then the other related question is one of logic. My cart will have a collection of receipts to post or update against the receipt table. I can retrieve a collection of receipts to compare against.
What is the best way to do this. Iterate through the cart collection or iterate the receipt collection. HOw can I identify easily the receipts that the user wants deleted nased on the information between the two carts?
Is there a php method to compare the two collections (I could convert to array) and get the id's of the records to delete.
I'm going round in circles here
Any advice appreciated
There's a lot of issues you are raising up in one questions... but I suggest that you look at the object that's getting back from $reservation->invoice. You suggested that reservations and invoices are two separate but one-to-many related entities, so when you call $reservation->invoices
, Laravel helps you out by returning a collection of invoice models, so you can iterate through them and update. Also you might also want to look at how you can utilize the relationship method ($reservation->invoices()
)
for your other question ... PHP Documentation is your friend See: array_diff and array_intersect type functions
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments