Laravel Save multiple table rows to the database

  laravel, php

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:

<form action="{{ action('[email protected]') }}" method="POST">
    <div class="form-group">
        <select class="form-control" name="student[]">
            @foreach( $students as $row )
            <option value="{{ $row->id }}">{{ $row->FirstName }} {{ $row->LastName }}</option>

    <h2>List of Books Borrowed</h2>
    <div class="table-responsive">
        <table id="books-detail-table" class="table table-striped table-bordered" style="width: 100%;">
                        <button id="addBtn" type="button" class="btn btn-sm btn-outline-success">
                            <span data-feather="plus"></span>
            <tbody id="tbody">
                        <div class="form-group">
                            <select class="form-control" name="book[]">
                                @foreach( $books as $row )
                                <option value="{{ $row->id }}">{{ $row->title}}</option>

                        <div class="form-group">
                            <input type="number" class="form-control form-control-sm" name="quantity[]" placeholder="Qty" required>

    <div class="btn-toolbar mb-2 mb-md-0">
        <div class="btn-group mr-2">
            <button type="submit" class="btn btn-success">
                <span data-feather="save"></span>

And here’s my controller for reference:
[email protected]

    public function store(Request $request)
        $timeToday = Carbon::now();
        $this->validate($request, [
            'student_id'   => 'required',
            'book_id'      => 'required',
            'quantity'     => 'required',

        $student_id    = $request->input('student[]');
        $book_id       = $request->input('book[]');
        $quantity      = $request->input('quantity[]');
        $date_borrowed = $timeToday->toDateTimeString();
        $return_date   = 0;
        $status        = "ACTIVE";

        for ($i = 0; $i < count($student_id); $i++) {
            $data = [
                'student_id' => $student_id[$i],
                'book_id' => $book_id[$i],
                'quantity' => $quantity[$i],
                'date_borrowed' => $date_borrowed[$i],
                'return_date' => $return_date[$i],
                'status' => $status,

        return redirect('/borrow_books')->with('success', 'Successfully borrowed a book.');

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.

Source: Laravel

Leave a Reply