我在laravel中有一个售票系统,我的问题是我的站点中有多个输入字段,分别是title
fname
lasname
,这些输入字段来自多少乘客的输入包括在内。现在,我有两个表,分别是booking
和booking_details
。我要将联系方式和手机号码(如下所示)保存到bookings
表中,并保存其他输入字段,例如标题< / strong>引用booking_details
的{{1}}表中的名...。我已经完成数据库的设置以及两个表之间的关系,现在我的问题是在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);
}
booking_details表
Route::post('saveBooking', 'FlightsController@addBooking');
预订表
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');
});
}
答案 0 :(得分:2)
样本方法:
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();
}
答案 1 :(得分:0)
公共功能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();
}