I have a form where I submit multiple values. It's a dynamic form where the fields differ each time, but onlt the quantity of the fields, not the fields itself. I am using the following to insert all data in the pivot table, however I hardly think this is a good way of going about this. Is there a good way of accomplishing this without using a foreach perhaps? Right now for only 3 exercises
I'm doing 12 queries? xD
The pivot table looks like this:
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
)
And here is the code:
$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]]);
}