1

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');
    });
}
8
  • hey from where you get $split1 variable in blade. And one thing is when you looping the array that time you have to put the array value ($i).like below is an example @for ($i=0; $i <= 4; $i++) <div class="row"> <div class="col-md-2"> <input type="text" name="properties[{{ $i }}][key]" class="form-control" value="{{ old('properties['.$i.'][key]') }}"> </div> Commented Sep 18, 2019 at 9:16
  • its like input for how many passengers should book. Like if you put 2 in the textbox for how many passesngers in the previous blade, it generates a 2 input fields for passengers Commented Sep 18, 2019 at 9:21
  • you want multiple rows in booking details table with first name last name using single booking id?? Commented Sep 18, 2019 at 9:23
  • booking_detail(booking_id,passenger_id - or passenger_name) would be a standard approach. Commented Sep 18, 2019 at 9:24
  • yes sir. I didint put the button yet in my blade because i was confused. @zahidhasanemon Commented Sep 18, 2019 at 9:24

2 Answers 2

3

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();
}
Sign up to request clarification or add additional context in comments.

6 Comments

it returns me an error sir of "The POST method is not supported for this route. Supported methods: GET, HEAD."
thats your route problem.
i think there is no problem with my route sir?
maybe i should add csrf token
remove the / in the form action url. if that does not resolves the issue try with a route instead of url
|
0

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.