[英]Laravel - error saving record with one to many relationship
Struggling to update my code with laravel relationships. 努力与laravel关系更新我的代码。
I have two tables. 我有两张桌子。 Customer and Reservations with Customer having a hasMany relationship with Reservation and Reservation having a belongs to relationship with Customer. 客户和与客户之间具有hasMany关系的预订和与客户之间具有属于关系的预订和预订。
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. 如果我尝试使用此代码,则会收到错误消息, Field 'customer_id' doesn't have a default value
-数据库表允许为null,并且未设置验证规则,因此假设这与我已建立的关系有关。
`$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();
错误发生在$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. 然后,我需要使用创建的$ reservation在产品链接表中创建条目,因此需要能够访问新创建的预订及其与产品的关系。
I can create the entry using $customer->reservation()->save($reservation);
我可以使用$customer->reservation()->save($reservation);
创建条目$customer->reservation()->save($reservation);
but then I don't seem to have a $reservation object to work with (or do I?) 但是然后我似乎没有可使用的$ reservation对象(或者我可以吗?)
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);
似乎有效的唯一方法是$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: 更新:在mysql中重新创建了我的表-现在我可以按预期保存原始预订记录,然后使用以下方式将其与客户记录相关联:
$reservation->customer()->associate($customer)
This is taking a while for it to sink in with me! 这需要一段时间才能融入我的生活!
PART 1 第1部分
since Customer hasMany Reservations, you need the customer id before you can add a reservation. 由于客户有很多预订,因此您需要客户ID才能添加预订。
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();
然后, $reservation->save();
should succeed now. 现在应该成功。
PART 2 第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?)" “我可以使用$ customer-> reservation()-> save($ reservation)创建条目;但是然后我似乎没有可使用的$ reservation对象(或者我可以吗?)”
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) (尽管我认为您可能需要保留ID,所以上面的第一种方法更好)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.