samedi 24 avril 2021

How to pass the date within an array to the database in laravel

I have made the following migration in Laravel:

public function up()
{
    Schema::create('attendances', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->bigInteger('student_id');
        $table->date('att_date')();
        $table->string('status');
        $table->timestamps();
    });
}

My Form in Blade look like this

<form method="post" action="" enctype="multipart/form-data">
  @csrf 
  <div class="row">
    <div class="col-md-4"></div>
    <div class="form-group col-md-4">
      <strong>Date : </strong>
      <input class="date form-control"  type="text" id="datepicker" name="att_date[]">
    </div>
  </div>
  <div class="mb-3">
    <table>
      <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Level</th>
        <th>Status</th>
      </tr>
      @foreach($students_att as $student)
        <tr>
          <td></td>
          <td></td>
          <td></td>
          <td>
            <input type="hidden" id="custId" name="student_id[]" value="">
          </td>
          <td>
            <select name="status[]">
              <option value="0">No</option>
              <option value="1">Yes</option>
            </select>
          </td>
        </tr>
      @endforeach
    </table>
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>

<script type="text/javascript">
  $('#datepicker').datepicker({
    autoclose: true,
    format: 'yyyy-mm-dd'
  });
</script>

and I have made my Controller like this:

public function sumbit(Request $request)
{
    /* $submit = new Attendance;
    $submit->student_id = $request->get('student_id');
    // $submit->att_date = $request->get('att_date');
    // $submit->status = $request->get('status');
    $submit->save();
    return redirect('att'); */

    $studentID = $request->input('student_id', []);
    $studentDate = $request->input('att_date', []);
    $studentStatus = $request->input('status', []);
    
    $students = [];
    
    foreach ($studentID as $index => $student) {
        $students[] = [
            "student_id" => $studentID[$index],
            "att_date" => $studentDate[$index],
            "status" => $studentStatus[$index],
        ];
    }
    
    $create = Attendance::insert($students);
}

so I want when i submit my form, it must be record the same date that i used by date picker to every input that show in the following image to my database image

but when i did this procedure, i got this error (ErrorException Undefined offset: 1) the error in this line in my controller line

"att_date" => $studentDate[$index],

How can Ii fix this error please help



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3xjaeQW
via IFTTT

Aucun commentaire:

Enregistrer un commentaire