从laravel 5表单中插入多个选定值

时间:2016-07-23 09:41:25

标签: mysql database forms laravel-5.2

我在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创建的多个选定字段,我想将其存储在数据库中,但据我所知,数据库中的每条记录都不允许多个值。如何使其工作?请帮助。

2 个答案:

答案 0 :(得分:2)

在多个月中,使用名称作为name="month[]"之类的数组,这样您就会收到一个包含月份的数组。然后,您可以迭代此数组,并在每个月在db中插入一条记录。您可以在foreach月创建插入查询,也可以使用批量插入。

public function store(Request $request)

{

$ inputs = $ request-&gt; 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);

}

享受编码:)

答案 1 :(得分:0)

在提交表单时,select2始终返回数组中的选定选项。您可以使用implode将数组转换为字符串,这样您就可以将结果存储为&#39; january,may&#39;在月场。