alierfani
3 years ago

How to insert multiple equal records into database from multiple form inputs?

Posted 3 years ago by alierfani

I have the following schema for users education:

Schema::create('educations', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->index()->unsigned();
            $table->string('study_field');
            $table->string('education_level');
            $table->string('location');
            $table->timestamp('start');
            $table->timestamp('end');
            $table->timestamps();
        });

So in the corresponding form I need 4 or 5 set of input fields like this:

<div class="row">
                <section class="col-md-6">
                    {!! Form::label('education_level', 'education level', ['class' => 'control-label']) !!}
                    <div class="input-group">
                        <div class="input-group-addon">
                                <span class="glyphicon glyphicon-education"></span>
                        </div>
                    {!! Form::select('education_level', $levels , null , ['class' => 'form-control']) !!}
                    </div>
                </section>
                <section class="col-md-6">
                    {!! Form::label('study_field', 'study field', ['class' => 'control-label']) !!}
                    <div class="input-group">
                        <div class="input-group-addon">
                            <span class="glyphicon glyphicon-education"></span>
                        </div>
                        {!! Form::text('study_field', null, ['class' => 'form-control']) !!}
                    </div>
                </section>
            </div>
            <div class="row">
                    <section class="col-md-6">
                        {!! Form::label('location', 'location', ['class' => 'control-label']) !!}
                        {!! Form::text('location', null, ['class' => 'form-control']) !!}
                    </section>
                    <section class="col-md-3">
                        {!! Form::label('start', 'start, ['class' => 'control-label']) !!}
                        {!! Form::selectYear('start', $year, $year-40 ,null, ['class' => 'form-control']) !!}
                    </section>
                    <section class="col-md-3">
                        {!! Form::label('end', 'endل', ['class' => 'control-label']) !!}
                        {!! Form::selectYear('end', $year , $year-40 ,null, ['class' => 'form-control']) !!}
                    </section>
  </div>

The first set of these input fields will be required and the other sets will be optional. How can I implement this?

Please sign in or create an account to participate in this conversation.