Php Laravel同步多个附加动态值
我有一个提交多个值的表单。这是一种动态形式,其中字段每次都不同,但只是字段的数量,而不是字段本身。我使用下面的方法在透视表中插入所有数据,但是我认为这不是一个很好的方法。有没有一种不用foreach就可以实现的好方法?现在只有3个Php Laravel同步多个附加动态值,php,laravel,laravel-5,synchronization,pivot,Php,Laravel,Laravel 5,Synchronization,Pivot,我有一个提交多个值的表单。这是一种动态形式,其中字段每次都不同,但只是字段的数量,而不是字段本身。我使用下面的方法在透视表中插入所有数据,但是我认为这不是一个很好的方法。有没有一种不用foreach就可以实现的好方法?现在只有3个练习我在做12个查询?xD 透视表如下所示: CREATE TABLE `exercise_training` ( `id` int(10) UNSIGNED NOT NULL, `exercise_id` int(10) UNSIGNED NOT NULL,
练习
我在做12个查询?xD
透视表如下所示:
CREATE TABLE `exercise_training` (
`id` int(10) UNSIGNED NOT NULL,
`exercise_id` int(10) UNSIGNED NOT NULL,
`training_id` int(10) UNSIGNED NOT NULL,
`exercise_set` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`exercise_repeat` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`exercise_weight` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`exercise_time` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
)
代码如下:
$checkedSets = $request['exercise_sets']; // Array of `set` values where the `key` is the `exercise_id`
$checkedRepeats = $request['exercise_repeats'];// Array of `repeat` values where the `key` is the `exercise_id`
$checkedWeights = $request['exercise_weights'];// Array of `weight` values where the `key` is the `exercise_id`
$checkedTimes = $request['exercise_times'];// Array of `time` values where the `key` is the `exercise_id`
foreach($checkedSets as $key => $value)
{
$training->exercises()->syncWithoutDetaching([$key => ['exercise_set' => $value]]);
}
foreach($checkedRepeats as $key => $value)
{
$training->exercises()->syncWithoutDetaching([$key => ['exercise_repeat' => $value]]);
}
foreach($checkedWeights as $key => $value)
{
$training->exercises()->syncWithoutDetaching([$key => ['exercise_weight' => $value]]);
}
foreach($checkedTimes as $key => $value)
{
$training->exercises()->syncWithoutDetaching([$key => ['exercise_time' => $value]]);
}
从
谢谢,我已经试过了,但没有成功,但可能是我做错了。我已经有一段时间没有对此进行研究了,稍后我将再次检查:)
You may also pass additional intermediate table values with the IDs:
$user->roles()->sync([1 => ['expires' => true], 2, 3]);