0

My framework is Laravel 5.3, How to create array in HTML and insert array to Database in PHP?

For Example $passenger is 2.

In HTML:

@for($i = 1; $i <= $passenger; $i++)
<li id="passenger-adult-{{ $i }}">
    <fieldset>
        <legend>Passenger</legend>
        <div class="row">
            <div class="col-md-3 col-md-offset-3">
                <div class="form-group">
                    <label>Name</label>
                    <input class="form-control" type="text" name="name[]" required/>
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label>Family</label>
                    <input class="form-control" type="text" name="family[]" required/>
                </div>
            </div>
        </div>
        .
        .
        .
    </fieldset>
</li>
@endfor

After send passenger data Array is:

Array
(
  [_method] => PATCH
  [_token] => OwNvaXUvoGAKL25UN85GBiV7pN5I2ILEXw7vZrMa
  [name] => Array
    (
        [0] => amir
        [1] => ali
    )

  [family] => Array
    (
        [0] => hesari
        [1] => ahmadi
    )
  .
  .
  .
)

But! I need create array in this case, for insert to database:

Array
(
  [_method] => PATCH
  [_token] => OwNvaXUvoGAKL25UN85GBiV7pN5I2ILEXw7vZrMa
  [passenger] => Array
    (
        [name] => amir
        [family] => hesari
        .
        .
        .
    )

  [passenger] => Array
    (
        [name] => ali
        [family] => ahmadi
        .
        .
        .
    )
)

1 Answer 1

1

change this code

 <input class="form-control" type="text" name="name[]" required/>

to

  <input class="form-control" type="text" name="passenger[{{$i}}][name]" required/>

change this

<input class="form-control" type="text" name="family[]" required/>

to

<input class="form-control" type="text" name="passenger[{{$i}}][family]" required/>
Sign up to request clarification or add additional context in comments.

Comments

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.