简体   繁体   中英

How to save multiple inputs in laravel in mysql database?

I have a ticketing system in laravel and my problem is I have multiple input fields in my site which are title fname lasname and that came from the inputs of how many passengers are included. Now I have two tables which are booking and booking_details . I want to save the Contact Details and Mobile Number (which are shown below) to bookings table, and the other input fields like the Title First Name ... in the booking_details table referencing the booking_id . I already finished setting the database and the relations of the two tables and now my problem is to save multiple names of the passengers in my booking_details table? That is my only problem. I tried different ways like using the array but I can't solve it. Maybe the syntax or logic is wrong. Can someone tell me what to do? Thanks in advance.

My Form.

 <form method="POST" action="{{url("/saveBooking")}}">
                        <div class="form-row">
                            <div class="col-sm-6 form-group">
                                <input name="email" class="form-control" id="email" required="" placeholder="Enter Email" type="text">
                            </div>
                            <div class="col-sm-6 form-group">
                                <input name="mobile_no" class="form-control" data-bv-field="number" id="mobileNumber" required="" placeholder="Enter Mobile Number" type="text">
                            </div>
                        </div>
                        <p class="text-info">Your booking details will be sent to this email address and mobile number.</p>

                            @for ($i = 0 ; $i<$split1 ; $i++)
                            <button style="margin: 5px;" type="button" class="btn btn-sm btn-info collapsible" >Passenger <?php echo $i + 1 ?></button>
                            <div class="form-row content"  style=" display: none;margin-top:7px;" >

                                <div class="col-sm-2 form-group" >
                                    <select class="custom-select" id="title" name="title[]" required="">
                                        <option value="">Title</option>
                                        <option>Mr</option>
                                        <option>Ms</option>
                                        <option>Mrs</option>
                                    </select>
                                </div>
                                <div class="col-sm-5 form-group">
                                    <input  name="fname[] "class="form-control" id="firstName" required="" placeholder="Enter First Name" type="text">
                                </div>
                                <div class="col-sm-5 form-group">
                                    <input   name="lname[]" class="form-control" data-bv-field="number" id="lastName" required="" placeholder="Enter Last Name" type="text">
                                </div>

                                <div id="new_chq">

                                </div>
                                <input type="hidden" value="1" id="total_chq">


                            </div>
                            @endfor
                        </div>
                        <br>
                        <button type="submit" class="btn btn-block btn-primary">Book</button>
                    </form>

Controller

public function addBooking(Request $request){

    $mobile_no = $request->input('mobile_no');
    $email = $request->input('email');

    $data = $request->all();

    $finalArray = array();
    foreach($data as $key=>$value){
        array_push($finalArray, array(
            'title'=>$value['title'],
            'fname'=>$value['fname'],
            'lastname'=>$value['lastname'])

);
    }

    BookingDetails::insert($finalArray);
}

Route

Route::post('saveBooking', 'FlightsController@addBooking');

booking_details table

public function up()
{
    Schema::create('booking_details', function (Blueprint $table) {
        $table->bigInteger('booking_id')->unsigned();
        $table->string('title');
        $table->string('fname');
        $table->string('lastname');
        $table->timestamps();
    });

    Schema::table('booking_details', function($table) {
        $table->foreign('booking_id')
              ->references('booking_id')->on('bookings')
              ->onDelete('cascade');
    });
}

bookings table

public function up()
{
    Schema::create('bookings', function (Blueprint $table) {
        $table->bigIncrements('booking_id');
        $table->bigInteger('flight_id')->unsigned();
        $table->string('email');
        $table->string('mobile_no');
        $table->integer('seat_no');
        $table->timestamps();
    });

    Schema::table('bookings', function($table) {
        $table->foreign('flight_id')
              ->references('flight_id')->on('flights')
              ->onDelete('cascade');
    });
}

Your data is coming as array from the form. So loop over the array and insert values in each iteration. Sample Approach:

public function addBooking(Request $request)
{
    $booking = Booking::create([
        'mobile_no' => $request->mobile_no,
        'email' => $request->email,
        //add if you want you add any more column
    ]);

    foreach ($request->title as $key => $value) {
        BookingDetails::create([
            'booking_id' => $booking_id,
            'title' => $request->title[$key],
            'fname' => $request->fname[$key],
            'lname' => $request->lname[$key],
            //other columns
        ]);
    }

    return->back();
}

public function addBooking(Request $request){

    $booking = Booking::create([
        'mobile_no' => $request->mobile_no,
        'email' => $request->email,
        //add if you want you add any more column
    ]);
$bookings=[];

    foreach($request->title as $key => $value){
        array_push(bookings,[
            'booking_id' => $booking_id,
            'title' => $request->title[$key],
            'fname' => $request->fname[$key],
            'lname' => $request->lname[$key]
        ]);
    }
   BookingDetails::insert($bookings);


    return->back();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM