如何在 mysql 数据库中的 laravel 中保存多个输入?

我在 laravel 中有一个票务系统,我的问题是我的网站中有多个输入字段,这些字段title fname lasname来自包含多少乘客的输入。现在我有两个表,它们是bookingbooking_details。我想将联系人详细信息手机号码(如下所示)保存到bookings,以及引用. 我已经完成了数据库和两个表的关系的设置,现在我的问题是在我的 booking_detailsbooking_idbooking_details桌子?那是我唯一的问题。我尝试了不同的方法,比如使用数组,但我无法解决它。也许语法或逻辑是错误的。有人可以告诉我该怎么做吗?提前致谢。

http://img2.mukewang.com/61e13ebc0001b30f11650872.jpg

我的表格。


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


智慧大石
浏览 160回答 2
2回答

慕运维8079593

您的数据来自表单的数组。因此,循环遍历数组并在每次迭代中插入值。示例方法:public function addBooking(Request $request){&nbsp; &nbsp; $booking = Booking::create([&nbsp; &nbsp; &nbsp; &nbsp; 'mobile_no' => $request->mobile_no,&nbsp; &nbsp; &nbsp; &nbsp; 'email' => $request->email,&nbsp; &nbsp; &nbsp; &nbsp; //add if you want you add any more column&nbsp; &nbsp; ]);&nbsp; &nbsp; foreach ($request->title as $key => $value) {&nbsp; &nbsp; &nbsp; &nbsp; BookingDetails::create([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'booking_id' => $booking_id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'title' => $request->title[$key],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'fname' => $request->fname[$key],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'lname' => $request->lname[$key],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //other columns&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; }&nbsp; &nbsp; return->back();}

牛魔王的故事

公共函数 addBooking(Request $request){&nbsp; &nbsp; $booking = Booking::create([&nbsp; &nbsp; &nbsp; &nbsp; 'mobile_no' => $request->mobile_no,&nbsp; &nbsp; &nbsp; &nbsp; 'email' => $request->email,&nbsp; &nbsp; &nbsp; &nbsp; //add if you want you add any more column&nbsp; &nbsp; ]);$bookings=[];&nbsp; &nbsp; foreach($request->title as $key => $value){&nbsp; &nbsp; &nbsp; &nbsp; array_push(bookings,[&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'booking_id' => $booking_id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'title' => $request->title[$key],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'fname' => $request->fname[$key],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'lname' => $request->lname[$key]&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; }&nbsp; &nbsp;BookingDetails::insert($bookings);&nbsp; &nbsp; return->back();}
打开App,查看更多内容
随时随地看视频慕课网APP