简体   繁体   中英

Laravel - error saving record with one to many relationship

Struggling to update my code with laravel relationships.

I have two tables. Customer and Reservations with Customer having a hasMany relationship with Reservation and Reservation having a belongs to relationship with Customer.

The reservation table also has a many to many relationship with a product table via a link table.

I obtain the customer record ok and I now need to create a reservation for the customer.

I'm assuming the process is: create the reservation object, attach the reservation to the customer and then link to the product. (the reservation table has a few relationships but I'll get this working for now)

If I try this code I get an error Field 'customer_id' doesn't have a default value - the database table allows null and there are no validation rules set so assume this is to do with the relationship I've set up.

`$reservation = new Reservation;

        $reservation->play_date=$play_date->format('Y-m-d');
        $reservation->booked_date=$booked_date->format('Y-m-d');
        $reservation->am_tee=$am_time->format('H:i:s');
        $reservation->pm_tee=$pm_time->format('H:i:s');
        $reservation->save();

        $customer->reservation()->associate($reservation);`

The error occurs with $reservation->save();

I then need to use the created $reservation to create entries in the product link table so need to be able to access the newly created reservation and it's relationship with products.

I can create the entry using $customer->reservation()->save($reservation); but then I don't seem to have a $reservation object to work with (or do I?)

I'm very confused by the relationships so grateful for all help to understand how to get this to work

Thanks

here's my relationships in the models:

    Customer Class:

        public function reservation() {
                return $this->hasMany('Reservation');
            }

Reservation Class:

public function customer() {
        return $this->belongsTo('Customer');
    }

The only method that appears to work is $customer->reservation()->save($reservation); - so I assume this is the correct method and will have to rework my code. associate()

update: recreated my table in mysql - I can now save the original reservation record as expected and then associate to the customer record using:

$reservation->customer()->associate($customer)

This is taking a while for it to sink in with me!

PART 1

since Customer hasMany Reservations, you need the customer id before you can add a reservation.

on your code,

you need to get the customer first that owns the reservation

$customer = Customer::find($id);

now,

$reservation = new Reservation;

$reservation->customer_id = $customer->id;    
$reservation->play_date=$play_date->format('Y-m-d');
$reservation->booked_date=$booked_date->format('Y-m-d');
$reservation->am_tee=$am_time->format('H:i:s');
$reservation->pm_tee=$pm_time->format('H:i:s');
$reservation->save();

then, the $reservation->save(); should succeed now.

PART 2

on your question:

"I can create the entry using $customer->reservation()->save($reservation); but then I don't seem to have a $reservation object to work with (or do I?)"

you can get all reservations of a customer by using

$reservations = $customer->reservation->get(); 
//where customer is the one you had on Customer::find($id);

you can then loop on the reservations one by one. (although i think you might want the reservation ids, so the first approach above is better)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM