Обновление вызывается дважды. Дамп "hello" выводит 2 результата и ошибку
Illuminate\Contracts\Container\BindingResolutionException: Target class [config] does not exist.
Без сброса не возникает ошибка разрешения привязки, но обновление вызывается снова.
Контроллер
public function update(SchedulesRequest $scheduleRequest, ScheduleDayTimesRequest $scheduleDayTimesRequest, Schedule $schedule)
{
$schedulesData = $scheduleRequest->validated();
$dayTimesData = $scheduleDayTimesRequest->validated();
//Updating existing upcoming schedule
if($schedule->start_date->toDateTimeString() >= now()) {
$schedule->update($schedulesData);
foreach($dayTimesData['schedule_day_times'] as $dayTime) {
$schedule->dayTimes()->updateOrCreate(['id' => $dayTime['id']], ['day_name' => $dayTime['day_name'], 'start_time' => $dayTime['start_time'], 'end_time' => $dayTime['end_time']]);
}
}
//Updating existing current schedule
else if($schedule->start_date->toDateTimeString() < now() && $schedule->end_date->toDateTimeString() >= now()) {
$schedule->update(['end_date' => $scheduleRequest['start_date']]);
$scheduleRecord = Schedule::create($schedulesData);//Change start date
$scheduleRecord->dayTimes()->createMany($dayTimesData['schedule_day_times']);
}
//Removing scheduledaytimes that have null in both start_time and end_time
$deleteIdList = collect($scheduleDayTimesRequest->all())->get('delete_id_list', null);
if($deleteIdList != null)
$schedule->dayTimes()->whereIn('id', $deleteIdList)->delete();
return redirect($schedule->path());
}
Путь в модели:
class Schedule extends Model
{
//Some lines of code
public function path()
{
return '/schedules' . $this->id;
}
}
В файле запроса есть несколько правил проверки.
ScheduleTest
/** @test */
public function schedule_can_be_updated()
{
$this->withoutExceptionHandling();
$this->post('/schedules', $this->data());
$schedule = Schedule::first();
$response = $this->patch('/schedules/'. $schedule->id, $this->updateData());
$schedule->refresh();
$this->assertEquals(6, $schedule->location_id);
$this->assertEquals(6, $schedule->service_id);
$this->assertEquals(6, $schedule->user_id);
$this->assertEquals(Carbon::tomorrow()->toDateTimeString(), $schedule->start_date->toDateTimeString());
$this->assertEquals(Carbon::now()->addDays(5)->toDateTimeString(), $schedule->end_date->toDateTimeString());
$this->assertEquals(5, $schedule->dayTimes()->find(1)->day_name);
$this->assertEquals(7, $schedule->dayTimes()->find(4)->day_name);
$this->assertCount(4, $schedule->dayTimes()->get());
$this->assertEquals(Carbon::parse('08:00 am')->format('H:i'), $schedule->dayTimes()->first()->start_time);
$this->assertEquals(Carbon::parse('11:00 am')->format('H:i'), $schedule->dayTimes()->first()->end_time);
$this->assertEquals(2, $schedule->created_by_user_id);
$response->assertRedirect($schedule->path());
}
private function updateData()
{
return [
'location_id' => 6,
'service_id' => 6,
'user_id' => 6,
'start_date' => Carbon::tomorrow(),
'end_date' => Carbon::now()->addDays(5),
'schedule_day_times' => array(array('id' => 1, 'day_name' => 5, 'start_time' => Carbon::parse("08:00 am")->format("H:i"), 'end_time' => Carbon::parse("11:00 am")->format("H:i")),
array('id' => 2, 'day_name' => 2, 'start_time' => Carbon::parse("01:00 pm")->format("H:i"), 'end_time' => Carbon::parse("02:00 pm")->format("H:i")),
array('id' => 3, 'day_name' => 2, 'start_time' => Carbon::parse("03:00 pm")->format("H:i"), 'end_time' => Carbon::parse("05:00 pm")->format("H:i")),
array('id' => 4, 'day_name' => 7, 'start_time' => Carbon::parse("06:00 pm")->format("H:i"), 'end_time' => Carbon::parse("08:00 pm")->format("H:i")),
),
'created_by_user_id' => 2,
];
}
Тест не пройден. Но если я заменим метод обновления приведенным ниже кодом, тест пройден.
public function update(SchedulesRequest $scheduleRequest, ScheduleDayTimesRequest $scheduleDayTimesRequest, Schedule $schedule)
{
$dayTimesData = $scheduleDayTimesRequest->validated();
$schedule->update($scheduleRequest->validated());
foreach($dayTimesData['schedule_day_times'] as $dayTime) {
$schedule->dayTimes()->updateOrCreate(['id' => $dayTime['id']], ['day_name' => $dayTime['day_name'], 'start_time' => $dayTime['start_time'], 'end_time' => $dayTime['end_time']]);
}
$deleteIdList = collect($scheduleDayTimesRequest->all())->get('delete_id_list', null);
if($deleteIdList != null)
$schedule->dayTimes()->whereIn('id', $deleteIdList)->delete();
return redirect($schedule->path());
}
Тест выше пройден, но обновление по-прежнему вызывается дважды.