Laravel 4多对多更新

[英]Laravel 4 Many to Many update


I'm stuck in a problem and I can't find a solution to this, it's annoying me.

我陷入了一个问题,我无法找到解决方案,这让我烦恼。

I've two tables, one called contacts and the other one called phonebooks and they are linked with a third table called *contacts_phonebooks*, this is a many-to-many relationship summarize below:

我有两个表,一个称为联系人,另一个称为电话簿,它们与第三个名为* contacts_phonebooks *的表相关联,这是一个多对多关系总结如下:

  • contacts: id (pk)
  • 联系人:id(pk)

  • phonebooks: id (pk)
  • 电话簿:id(pk)

  • contacts_phonebooks: contactid (fk), phonebooksid (fk) Pretty simple and clear, and it works.
  • contacts_phonebooks:contactid(fk),phonebooksid(fk)非常简单明了,而且很有效。

I'm using Laravel 4 and Eloquent ORM, everythings works fine when I've to fetch it, insert it and delete it but when I need to update a contact I fail miserably. I've a form that has a number of checkboxes that represent all the phonebooks (every checkbox has phonebook[] as name) so when you check one of those the phonebook id will be saved in the *contacts_phonebooks* with the contact id. The problem is that this is not true! I mean when I run this code:

我正在使用Laravel 4和Eloquent ORM,当我要获取它,插入并删除它时,每件事都可以正常工作但是当我需要更新一个联系人时,我失败了。我有一个表单,其中包含代表所有电话簿的复选框(每个复选框都有电话簿[]作为名称),因此当您检查其中一个时,电话簿ID将保存在带有联系人ID的* contacts_phonebooks *中。问题是这不是真的!我的意思是当我运行此代码时:

$contact = Contact::find($id);
$contact->contact_name = Input::get('newCName');
$contact->contact_surname = Input::get('newCSurname');
$contact->contact_email = Input::get('newCEmail');
$contact->contact_phone = Input::get('newCPhone');
$contact->contact_birth = Input::get('newCDate');
$contact->phonebooks()->sync(Input::get('phonebook'));
if($contact->save())
{
    return "TEST DONE?";
}

It deletes every row in *contacts_phonebooks* associated with the contact id and save only the new one checked... This is weird I know, I try to explain it better. I want to update Mr.x and he actually is in "Stackoverflow" phonebook, I want to add him in "Nerd" phonebook so I click on update and I selected "Nerd", the other one is already selected.

它会删除与联系人ID相关联的* contacts_phonebooks *中的每一行,并仅保存已检查的新行...这很奇怪我知道,我试着更好地解释它。我想更新Mr.x,他实际上是在“Stackoverflow”电话簿中,我想在“Nerd”电话簿中添加他,所以我点击更新,我选择“Nerd”,另一个已经被选中。

When I update him the system deletes the "Stackoverflow" link and save ONLY the "Nerd" phonebook (with the code above) this things driving me crazy because Laravel 4 Doc says that you should use the sync() method in order to update a many-to-many relationship.

当我更新他时,系统会删除“Stackoverflow”链接并仅保存“书呆子”电话簿(上面的代码)这件事让我发疯,因为Laravel 4 Doc说你应该使用sync()方法更新一个多对多的关系。

I don't how how to solve it, I hope you will understand what's my problem.

我不知道如何解决它,我希望你能理解我的问题是什么。

Cheers.

3 个解决方案

#1


3  

The documentation says "The sync method accepts an array of IDs to place on the pivot table. After this operation is complete, only the IDs in the array will be on the intermediate table for the model:"

文档说“同步方法接受一个I​​D数组放在数据透视表上。完成此操作后,只有数组中的ID将位于模型的中间表上:”

So what I think you are probably looking for is attach().

所以我认为你可能正在寻找的是attach()。

$contact->phonebooks()->attach(Input::get('phonebook'));

Then you will have to use detach() to remove him from the other.

然后你将不得不使用detach()将他从另一个中删除。

#2


1  

As stated in the docs: The sync method accepts an array of IDs to place on the pivot table.

如文档中所述:sync方法接受要放置在数据透视表上的ID数组。

#3


0  

Your pivot table should be named contact_phonebook and it specifies that in Laravel's documentation.

您的数据透视表应命名为contact_phonebook,并在Laravel的文档中指定。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2013/07/29/c7212d2162815939754eafde02fa7749.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号