title: Laravel Save multiple table rows to the database
body: <p>I'm trying to save multiple table rows to the database. And when I try run my code, nothing saves to the database. Here's my code for the blade template:</p>
  <pre><code>&lt;form action=&quot;{{ action('BorrowBookController@store') }}&quot; method=&quot;POST&quot;&gt;
      @csrf
      &lt;div class=&quot;form-group&quot;&gt;
          &lt;select class=&quot;form-control&quot; name=&quot;student[]&quot;&gt;
              @foreach( $students as $row )
              &lt;option value=&quot;{{ $row-&gt;id }}&quot;&gt;{{ $row-&gt;FirstName }} {{ $row-&gt;LastName }}&lt;/option&gt;
              @endforeach
          &lt;/select&gt;
      &lt;/div&gt;
  
      &lt;h2&gt;List of Books Borrowed&lt;/h2&gt;
      &lt;div class=&quot;table-responsive&quot;&gt;
          &lt;table id=&quot;books-detail-table&quot; class=&quot;table table-striped table-bordered&quot; style=&quot;width: 100%;&quot;&gt;
              &lt;thead&gt;
                  &lt;tr&gt;
                      &lt;th&gt;Title&lt;/th&gt;
                      &lt;th&gt;Quantity&lt;/th&gt;
                      &lt;th&gt;
                          &lt;button id=&quot;addBtn&quot; type=&quot;button&quot; class=&quot;btn btn-sm btn-outline-success&quot;&gt;
                              &lt;span data-feather=&quot;plus&quot;&gt;&lt;/span&gt;
                          &lt;/button&gt;
                      &lt;/th&gt;
                  &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody id=&quot;tbody&quot;&gt;
                  &lt;tr&gt;
                      &lt;td&gt;
                          &lt;div class=&quot;form-group&quot;&gt;
                              &lt;select class=&quot;form-control&quot; name=&quot;book[]&quot;&gt;
                                  @foreach( $books as $row )
                                  &lt;option value=&quot;{{ $row-&gt;id }}&quot;&gt;{{ $row-&gt;title}}&lt;/option&gt;
                                  @endforeach
                              &lt;/select&gt;
  
                          &lt;/div&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;div class=&quot;form-group&quot;&gt;
                              &lt;input type=&quot;number&quot; class=&quot;form-control form-control-sm&quot; name=&quot;quantity[]&quot; placeholder=&quot;Qty&quot; required&gt;
                          &lt;/div&gt;
                      &lt;/td&gt;
                      &lt;td&gt;&lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/tbody&gt;
          &lt;/table&gt;
  
      &lt;/div&gt;
      &lt;div class=&quot;btn-toolbar mb-2 mb-md-0&quot;&gt;
          &lt;div class=&quot;btn-group mr-2&quot;&gt;
              &lt;button type=&quot;submit&quot; class=&quot;btn btn-success&quot;&gt;
                  &lt;span data-feather=&quot;save&quot;&gt;&lt;/span&gt;
                  Submit
              &lt;/button&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  
  </code></pre>
  <p>And here's my controller for reference:
  <code>BorrowBookController@store</code></p>
  <pre><code>    public function store(Request $request)
      {
          $timeToday = Carbon::now();
          $this-&gt;validate($request, [
              'student_id'   =&gt; 'required',
              'book_id'      =&gt; 'required',
              'quantity'     =&gt; 'required',
          ]);
  
          $student_id    = $request-&gt;input('student[]');
          $book_id       = $request-&gt;input('book[]');
          $quantity      = $request-&gt;input('quantity[]');
          $date_borrowed = $timeToday-&gt;toDateTimeString();
          $return_date   = 0;
          $status        = &quot;ACTIVE&quot;;
  
          for ($i = 0; $i &lt; count($student_id); $i++) {
              $data = [
                  'student_id' =&gt; $student_id[$i],
                  'book_id' =&gt; $book_id[$i],
                  'quantity' =&gt; $quantity[$i],
                  'date_borrowed' =&gt; $date_borrowed[$i],
                  'return_date' =&gt; $return_date[$i],
                  'status' =&gt; $status,
              ];
              BorrowBook::create($data);
          }
  
          return redirect('/borrow_books')-&gt;with('success', 'Successfully borrowed a book.');
      }
  </code></pre>
  <p>I don't really know where my problem is. It's either on the controller or the blade template. How can I achieve in solving this kind of issue? I hope everyone can help me with this. Thank you.</p>