前提・実現したいこと
勤怠システムの作成をしております。
laravelで複数レコードににupdateをかけたいです。
updateをすると入力した値が全てNULLになって更新されてしまいます。
発生している問題・エラーメッセージ
エラーなどは吐かれてなく、更新後がNULLになるので更新後を入力した値にしたいです。
該当のソースコード
html
1<form action="/admin/update" method="POST" name="update"> 2 @method('PUT') 3 @csrf 4 <table id="management" class="table table-bordered "> 5 <tr> 6 <th class='month'>{{$month->month}}</th> 7 <th colspan="3" style="text-align:center">平日</th> 8 <th colspan="2" style="text-align:center">休日</th> 9 <th colspan="6" style="text-align:center">勤怠</th> 10 </tr> 11 <tr> 12 <th>日付</th> 13 <!-- <th>曜日</th> --> 14 <th>始業時刻</th> 15 <th>終業時刻</th> 16 <th>休憩時間</th> 17 <th>休日</th> 18 <th>休日深夜</th> 19 <th>休暇</th> 20 <th>欠勤</th> 21 <th>遅刻</th> 22 <th>早退</th> 23 <th>休出</th> 24 <th>振休</th> 25 </tr> 26 @foreach($edit_list as $edit) 27 <tr> 28 <td>{{$edit->calendar_id}}</td> 29 <td><input class="form-control" type="time" step="900" name="opening_time[{{$edit->id}}]" value="{{$edit->opening_time}}"></td> 30 <td><input class="form-control" type="time" step="900" name="ending_time[{{$edit->id}}]" value="{{$edit->ending_time}}"></td> 31 <td><input class="form-control" type="time" step="900" name="break_time[{{$edit->id}}]" value="{{$edit->break_time}}"></td> 32 <td><input class="form-control" type="time" step="900" name="holiday_time[{{$edit->id}}]" value="{{$edit->holiday_time}}"></td> 33 <td><input class="form-control" type="time" step="900" name="holiday_night[{{$edit->id}}]" value="{{$edit->holiday_night}}"></td> 34 <td> 35 <input type="hidden" name="holiday[{{$edit->id}}]" value="0"> 36 <input type="checkbox" name="holiday[{{$edit->id}}]" value="{{($edit->holiday)}}"> 37 </td> 38 <td> 39 <input type="hidden" name="adsence[{{$edit->id}}]" value="0"> 40 <input type="checkbox" name="adsence[{{$edit->id}}]" value="{{($edit->adsence)}}"> 41 </td> 42 <td> 43 <input type="hidden" name="late[{{$edit->id}}]" value="0"> 44 <input type="checkbox" name="late[{{$edit->id}}]" value="{{($edit->late)}}"> 45 </td> 46 <td> 47 <input type="hidden" name="leave_early[{{$edit->id}}]" value="0"> 48 <input type="checkbox" name="leave_early[{{$edit->id}}]" value="{{($edit->leave_early)}}"> 49 </td> 50 <td> 51 <input type="hidden" name="holiday_work[{{$edit->id}}]" value="0"> 52 <input type="checkbox" name="holiday_work[{{$edit->id}}]" value="{{($edit->holiday_work)}}"> 53 </td> 54 <td> 55 <input type="hidden" name="makeup_holiday[{{$edit->id}}]" value="0"> 56 <input type="checkbox" name="makeup_holiday[{{$edit->id}}]" value="{{($edit->makeup_holiday)}}"> 57 </td> 58 </tr> 59 <input type="hidden" name="id[{{$edit->id}}]" value="{{$edit->id}}"> 60 @endforeach 61 </table> 62 <div class="container mt-4 mb-5"> 63 <div class="row"> 64 <div class="col-3 offset-5"> 65 <button type="submit" class="btn btn-lg btn-outline-success">更新</button>
該当のソースコード
php
1public function update(Request $request){ 2 3 foreach($request->input('id') as $id){ 4 5 $update_culumn = [ 6 7 'opening_time' => $request->input('opening_time[$id]'), 8 'ending_time' => $request->input('ending_time[$id]'), 9 'break_time' => $request->input('break_time[$id]'), 10 'holiday_time' => $request->input('holiday_time[$id]'), 11 'holiday_night' => $request->input('holiday_night[$id]'), 12 'holiday' => $request->input('holiday[$id]'), 13 'adsence' => $request->input('adsence[$id]'), 14 'late' => $request->input('late[$id]'), 15 'leave_early' => $request->input('leave_early[$id]'), 16 'holiday_work' => $request->input('holiday_work[$id]'), 17 'makeup_holiday' => $request->input('makeup_holiday[$id]')]; 18 19 Management::where('id',$id)->update($update_culumn); 20 21 } 22 return redirect('/'); 23 }
該当のソースコード
php
1<?php 2 3namespace App; 4 5use Illuminate\Database\Eloquent\Model; 6 7class Management extends Model 8{ 9 protected $fillable = [ 10 11 'opening_time','ending_time','break_time', 12 'holiday_time','holiday_night','holiday', 13 'adsence','late','leave_early','holiday_work', 14 'makeup_holiday','project','memo','user_id', 15 'month_id','calendar_id','created_at','updated_at','year']; 16 17 18 public function user(){ 19 20 return $this->belongsTo(User::class); 21 } 22 23 public function month(){ 24 25 return $this->belongsTo(Month::class); 26 } 27}
試したこと
dd()でデバッグして$requestには期待した値が入っていることを確認しています。
$update_culumnをdd()すると中はNULLになっていて空の状態でupdateが
かかっていることまで把握しています。
補足情報(FW/ツールのバージョンなど)
php 7.3.6 laravel 5.8
初学者なため質問の意図や情報が足りない場合がありますので
遠慮なく仰っていただけると嬉しいです。
申し訳ありませんがご教示願います...
回答3件
あなたの回答
tips
プレビュー