【问题标题】:How to save multiple inputs in laravel in mysql database?如何在 mysql 数据库中的 laravel 中保存多个输入?
【发布时间】:2020-11-19 19:23:34
【问题描述】:

我在 laravel 中有一个票务系统,我的问题是我的网站中有多个输入字段 title fname lasname 来自输入的乘客数量。现在我有两张表,分别是bookingbooking_details。我想将 Contact DetailsMobile Number(如下所示)保存到 bookings 表,以及其他输入字段,如 Title 名字...在booking_details 表中引用booking_id。我已经完成了数据库和两个表的关系设置,现在我的问题是在我的booking_details 表中保存多个乘客姓名?那是我唯一的问题。我尝试了不同的方法,比如使用数组,但我无法解决它。也许语法或逻辑是错误的。有人可以告诉我该怎么做吗?提前致谢。

我的表单。

 <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>

控制器

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::post('saveBooking', 'FlightsController@addBooking');

booking_details 表

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

预订表

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

【问题讨论】:

  • 嘿,你从哪里获得刀片中的 $split1 变量。有一件事是当你循环数组时你必须把数组值 ($i).like 下面是一个例子 @for ($i=0; $i
  • 它类似于输入应该预订多少乘客。就像你在前一个刀片中有多少乘客的文本框中输入 2,它会为乘客生成 2 个输入字段
  • 您希望在预订详细信息表中有多个行,其中名字姓氏使用单个预订 ID??
  • booking_detail(booking_id,passenger_id - or passenger_name) 将是一种标准方法。
  • 是的,先生。我没有把按钮放在我的刀片上,因为我很困惑。 @zahidhasanemon

标签: php mysql laravel


【解决方案1】:

您的数据来自表单的数组。因此,循环遍历数组并在每次迭代中插入值。 示例方法:

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

【讨论】:

  • 它返回给我一个错误,先生 "The POST method is not supported for this route. Supported methods: GET, HEAD."
  • 那是你的路线问题。
  • 先生,我认为我的路线没有问题?
  • 也许我应该添加csrf令牌
  • 删除表单操作 url 中的 /。如果这不能解决问题,请尝试使用路由而不是 url
【解决方案2】:

公共函数 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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    • 2015-09-21
    • 1970-01-01
    • 2019-02-06
    • 2021-11-06
    相关资源
    最近更新 更多