0

I have a form in laravel 5 as below

@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

And this is the payment migration table,

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

The 'month' field in the form is a multiple selected field made with select2 which I want to store in the database but as far as I know each record in the database does not allow multiple values.How can I make it work? Please,help.

2 Answers 2

2

In the multiple select month, use name as array like name="month[]" so you will receive an array with month. Then, you can iterate this array and for each month insert a record in db. You can either create insert query foreach month or use batch insert.

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

}

Enjoy coding :)

Sign up to request clarification or add additional context in comments.

Comments

0

On submit the form select2 always return the selected options in array. You can convert the array in string using the implode so you can store the result as 'january,may' in month field.

2 Comments

Instead of storing the stings separated by comma in the same column ,can I store them in different row for the same username?
No you can't store them in different row for the same id in same table. If you want to store two records for two selected values you need to create another table which having only two fields payment_id and month. In loop of month array payment_id column store the newly inserted id and month column store the each month.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.