0
我有3種型號Laravel保存嵌套模式hasManyThrough
Event has many Shifts
Shift belongs to Event
Shift has many ShiftWorkers
ShiftWorker belongs to Shift
當我得到通過HTML表單一次保存所有機型這家店方法控制器:
public function store(EventsForm $request)
{
//dd($request->all());
// save event
$event = Auth::user()->events()->create($request->all());
$total_shifts = count($request->shift_start);
$shifts = [];
for ($s = 0; $s <= $total_shifts-1; $s++) {
array_push($shifts, new Shift(['shift_start' => $request->shift_start[$s], 'shift_end' => $request->shift_end[$s]]));
}
// save shifts
$event->shifts()->saveMany($shifts);
$workers = [];
for ($w = 0; $w <= $total_shifts-1; $w++) {
array_push($workers, new ShiftWorker(['quantity' => $request->quantity[$w], 'hour_wage' => $request->hour_wage[$w]]));
}
// save workers
$event->shifts()->shift_workers()->saveMany($workers);
我得到這個錯誤
調用未定義的方法Illuminate \ Database \ Query \ Builder :: shift_workers()
我如何保存我的第三個模型(ShiftWorker)?
我看,現在我知道我的問題是什麼,但現在要解決這個就更難了....我沒有同在每個班次中的換檔員數量....我如何將換檔員保存到他的班次......使用哪個循環?例如,如果第一班有一個shiftworker和第二班有2 .... – lewis4u
這是另一個問題,我想。你應該確定輪班內的輪班員工。請在上面標記正確的答案。 – schellingerht