laravel update, remove one to many relationship eloquent

Ray

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

Hao Luo

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Laravel save / update many to many relationship

From Dev

Laravel save many-to-many relationship in Eloquent mutators

From Dev

Laravel save one to many relationship

From Dev

Laravel Eloquent - Save/Update Related One-To-Many Relationship Data

From Dev

Laravel Eloquent: How to select not attached records in many to many relationship?

From Dev

Updating a One-To-Many Relationship in Laravel's Eloquent

From Dev

Laravel eloquent update one-many relationship

From Dev

Laravel 5 Eloquent Relationship - Has Many Though

From Dev

Laravel many to many relationship using Eloquent

From Dev

Need help to structure Eloquent Many to Many relationship (laravel 5.1)

From Dev

Laravel 5.1 Eloquent distant relationship with many to many models

From Dev

Laravel: one to many relationship

From Dev

Laravel 5 Eloquent count many to many relationship

From Dev

Laravel Eloquent Many to Many relationship using a pivot table

From Dev

Laravel Eloquent - Fetch data only if there is a match with data one to many relationship

From Dev

Laravel eloquent update one-many relationship

From Dev

Laravel 5 one to many eloquent relationship

From Dev

Laravel Eloquent one to many

From Dev

Laravel 5 Eloquent Relationship - Has Many Though

From Dev

Laravel Eloquent "Many-to-Many-to-One ?"

From Dev

Laravel eloquent one to many relationship in custom pivot tables

From Dev

Laravel 5.2 Eloquent - Model through Many-To-Many Relationship

From Dev

Is there a way to select columns using eloquent one to many relationship in laravel 5.2?

From Dev

one to many relationship in laravel

From Dev

Laravel Many-to-one relationship

From Dev

Laravel insert in one to many relationship

From Dev

How to get data for one to many relationship using Eloquent in Laravel?

From Dev

One to Many to One relationship in laravel eloquent

From Dev

Seeding Relationship one to many in Laravel

Related Related

  1. 1

    Laravel save / update many to many relationship

  2. 2

    Laravel save many-to-many relationship in Eloquent mutators

  3. 3

    Laravel save one to many relationship

  4. 4

    Laravel Eloquent - Save/Update Related One-To-Many Relationship Data

  5. 5

    Laravel Eloquent: How to select not attached records in many to many relationship?

  6. 6

    Updating a One-To-Many Relationship in Laravel's Eloquent

  7. 7

    Laravel eloquent update one-many relationship

  8. 8

    Laravel 5 Eloquent Relationship - Has Many Though

  9. 9

    Laravel many to many relationship using Eloquent

  10. 10

    Need help to structure Eloquent Many to Many relationship (laravel 5.1)

  11. 11

    Laravel 5.1 Eloquent distant relationship with many to many models

  12. 12

    Laravel: one to many relationship

  13. 13

    Laravel 5 Eloquent count many to many relationship

  14. 14

    Laravel Eloquent Many to Many relationship using a pivot table

  15. 15

    Laravel Eloquent - Fetch data only if there is a match with data one to many relationship

  16. 16

    Laravel eloquent update one-many relationship

  17. 17

    Laravel 5 one to many eloquent relationship

  18. 18

    Laravel Eloquent one to many

  19. 19

    Laravel 5 Eloquent Relationship - Has Many Though

  20. 20

    Laravel Eloquent "Many-to-Many-to-One ?"

  21. 21

    Laravel eloquent one to many relationship in custom pivot tables

  22. 22

    Laravel 5.2 Eloquent - Model through Many-To-Many Relationship

  23. 23

    Is there a way to select columns using eloquent one to many relationship in laravel 5.2?

  24. 24

    one to many relationship in laravel

  25. 25

    Laravel Many-to-one relationship

  26. 26

    Laravel insert in one to many relationship

  27. 27

    How to get data for one to many relationship using Eloquent in Laravel?

  28. 28

    One to Many to One relationship in laravel eloquent

  29. 29

    Seeding Relationship one to many in Laravel

HotTag

Archive