gpt4 book ai didi

mysql - 从 Laravel 5 表单中插入多个选定值

转载 作者:行者123 更新时间:2023-11-29 07:14:14 26 4
gpt4 key购买 nike

我在 laravel 5 中有一个表单,如下

@extends('users.home')




@section('content')



<form role="form" action="{{route('postPay')}}" method="post">
<div class="row">

<div class="col-md-12">
<fieldset class="group-border">

<legend class="group-border">Payment Record</legend>

<div class="row">
<div class="col-lg-4">
<div class="form-inline">

<label class="control-label">Current Date:</label>




<input type="text" class="date form-control" id="datepicker" name="date">
</div>
</div>
<div class="col-lg-4">
<div class="form-inline">
<!-- <label class="control-label">Month:</label> -->
<select style="width:250px" multiple="true" class="form-control input-md" id="js-example-basic-single" name="month">

<option value="january">January</option>
<option value="february">February</option>
<option value="march">March</option>
<option value="april">April</option>
<option value="may">May</option>
<option value="june">June</option>
<option value="july">July</option>
<option value="august">August</option>
<option value="september">September</option>
<option value="october">October</option>
<option value="november">November</option>
<option value="december">December</option>

</select>

</div>
</div>
<div class="col-lg-4">
<div class="form-inline">
<label class="control-label">Year:</label>
<input class="form-control input-md" type="text" name="year">
</div>
</div>
</div>
<br>
<div class="row">


<div class="col-lg-1">
<label for="">Customer</label>
</div>
<div class="col-lg-7">
<select class="form-control input-md" name="record_id">
@foreach($records as $record)
<option value="{{$record->id}}">{{$record->user_name}}</option>
@endforeach
</select>
</div>



<div style="float: left;" class="col-lg-4 text-left">
<div class="form-inline">
<label class="control-label">Due: </label>
<input class="form-control input-sm" type="text" name="due">
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-lg-1">
<label class="control-label">Amount:</label>
</div>
<div class="col-lg-7">
<input class="form-control input-sm" type="text" name="amount">
</div>
<div class="col-lg-4">
<div class="form-inline">
<label class="control-label">Advance:</label>
<input class="form-control input-sm" type="text" name="advance">
</div>
</div>
</div>



</fieldset>

</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-success btn-block">Submit</button>
</div>
<input type="hidden" name="_token" value="{{Session::token()}}"/>

</form>

@endsection

这是支付迁移表,

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePaymentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('payments', function (Blueprint $table) {
$table->increments('id');
$table->integer('record_id')->unsigned();
$table->date('date');
$table->string('month');
$table->integer('year');
$table->integer('amount');
$table->integer('advance');
$table->integer('due');
$table->dateTime('created_at');
$table->dateTime('updated_at');

$table->foreign('record_id')->references('id')->on('records');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('payments');
}
}

表单中的“月份”字段是用 select2 创建的多选字段,我想将其存储在数据库中,但据我所知,数据库中的每条记录不允许多个值。我怎样才能使其工作?请帮忙。

最佳答案

在多选月份中,使用 name 作为数组,如 name="month[]" 这样您将收到一个包含月份的数组。然后,您可以迭代该数组并为每个月在数据库中插入一条记录。您可以为每个月创建插入查询或使用批量插入。

public function store(Request $request)

{

$inputs = $request->all();

$months = $inputs['month'];

//Multiple insert queries
foreach ($months as $month) {
Payments::create([
'date' => $input['date'],
'year' => $inputs['year']
'month' => $month,
'amount' => $inputs['amount'],
'advance' => $inputs['advance'],
'due' => $inputs['due'],
'record_id' => $inputs['record_id']
]);
}
//-------------------------------------------------//
//Batch insert, use either one
$data = [];
foreach ($months as $month) {
$data[] = [
'date' => $input['date'],
'year' => $inputs['year']
'month' => $month,
'amount' => $inputs['amount'],
'advance' => $inputs['advance'],
'due' => $inputs['due'],
'record_id' => $inputs['record_id']
]
}

DB::table('payments')->insert($data);

}

享受编码的乐趣:)

关于mysql - 从 Laravel 5 表单中插入多个选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38540334/

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