Laravel 将数据透视表附加到具有多个值的表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23226802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:46:40  来源:igfitidea点击:

Laravel attach pivot to table with multiple values

laravellaravel-4eloquentpivot-table

提问by Duncan Ogle

Background

背景

I'm creating a database revolving around food allergies and I have a many to many relationship between foods and allergies. There is also a pivot value called severitywhich has a numerical number representing the severity of the allergy for that food item.

我正在创建一个围绕食物过敏的数据库,我在食物和过敏之间有很多关系。还有一个称为枢轴值severity,它有一个数字,表示该食品的过敏严重程度。

This link table looks like this;

这个链接表看起来像这样;

food_id|allergy_id|severity
-------|----------|--------
     1 |        1 |      3
     1 |        4 |      1
     2 |        2 |      1

The problem

问题

When trying to update the link table with Eloquent (where $allergy_idsis an array)

尝试使用 Eloquent 更新链接表时(其中$allergy_ids是数组)

$food->allergies()->attach($allergy_ids);

How would I go about adding multiple values to this pivot table at once along with the pivot values?

我将如何将多个值与枢轴值一起一次添加到这个数据透视表?

I can add all the allergy_id's for a particular food item in one go using the above line, but how can I also add in the severitycolumn at the same time with an array of various severity values? Maybe something like

我可以allergy_id使用上述行一次性添加特定食品的所有's,但如何同时添加severity具有各种严重性值的数组的列?也许像

$food->allergies()->attach($allergy_ids, $severity_ids);

Edit: There could be between 0-20 allergies for a specific food item, and a severity rating from 0-4 for each allergy, if this helps at all.

编辑:对特定食物可能有 0-20 种过敏,每种过敏的严重程度等级为 0-4,如果这有帮助的话。

回答by Nuno Rafael Figueiredo

You can.

你可以。

From this example in Docs (4.2, 5.0):

从文档中的这个例子(4.25.0):

$user->roles()->sync(array(1 => array('expires' => true)));

Hardcoded version for the first two rows:

前两行的硬编码版本:

$food = Food::find(1);
$food->allergies()->sync([1 => ['severity' => 3], 4 => ['severity' => 1]]);

Dynamically, with your arrays $allergy_ids and $severities in a compatible state (size and sort), you shall prepare your sync data before. Something like:

动态地,当您的数组 $allergy_ids 和 $severities 处于兼容状态(大小和排序)时,您应该事先准备好同步数据。就像是:

$sync_data = [];
for($i = 0; $i < count($allergy_ids); $i++))
    $sync_data[$allergy_ids[$i]] = ['severity' => $severities[$i]];

$food->allergies()->sync($sync_data);

回答by Jarek Tkaczyk

You can't do it like you' like so I suggest a simple loop:

你不能像你喜欢的那样做,所以我建议一个简单的循环:

foreach ($allergy_ids as $key => $id)
{
  $food->allergies()->attach($id, array_get($severity_ids, $key));
  // should you need a sensible default pass it as a 3rd parameter to the array_get()
}

workaround However if you wanted to attach multiple allergies with single severity level/id then you could do this:

解决方法但是,如果您想附加具有单一严重性级别/ID 的多个过敏症,那么您可以这样做:

$food->allergies()->attach($allergy_ids, array('severity' => $singleSeverityValue));

回答by My Brain

Easiest indeed is to attach with the extra data, like so:

最简单的确实是附加额外的数据,如下所示:

$retailer->paymentmethods()->attach($paymentmethod, array('currency' => $paymentmethod->currency));

change out the values for food allergy severity, but you get the hint... :-)

更改食物过敏严重程度的值,但您会得到提示... :-)

回答by Jesus Erwin Suarez

To assign user to the specific role, do this below.

要将用户分配给特定角色,请执行以下操作。

$user = App\User::find(1);
$user->roles()->attach($roleId);

Hope this helps

希望这可以帮助