间接修改重载属性-添加到数组

时间:2019-04-12 22:40:56

标签: laravel laravel-5

我有一个带有子数组的数组,我试图向其中添加一个新项目,但出现此错误

  

对重载属性App \ Workout :: $ exercises的间接修改   不会影响。

我正在尝试“ array_push”和“ array_unique”,但没有运气。

foreach ($workouts as $key => $workout) {
    $workoutHolder[$workout->day_no][] = $workout;
    $workout->exercises = unserialize($workout->exercises);
    $exercise_name = [];

    foreach ($workout->exercises as $k => $exercise) {
        if (!empty($exercise['id']) && !empty($workout->exercises['exercise']['id'])) {
            $get_exercise = Exercise::where('id', $workout->exercises['exercise']['id'])->first();

            if (!empty($workout->exercises['exercise'])) {
                $exercise_name[] = $get_exercise->title;
                $workout->exercises['exercise']['title'] = array_unique($exercise_name);
            }
        }
    }
}

目标是数组“ $ workout-> exercises ['exercise']”应创建一个新键“ title”,并使用“ $ get_exercise”的响应填充该键。

0 个答案:

没有答案