gpt4 book ai didi

php - 如何将laravel中的多个输入保存到mysql数据库中?

转载 作者:行者123 更新时间:2023-11-29 06:28:36 25 4
gpt4 key购买 nike

我在 Laravel 中有一个票务系统,我的问题是我的网站中有多个输入字段,它们是 title fname lasname 并且来自包含多少乘客的输入。现在我有两个表,分别是 bookingbooking_details。我想将联系方式手机号码(如下所示)保存到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>

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

预订表

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

最佳答案

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

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

关于php - 如何将laravel中的多个输入保存到mysql数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57988779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com