0

I want to be able to store multiple array values in one save operation. Is this possible?

I have a dynamic table that produces the following array:

    array:5 [▼
  "_token" => "cNvFpF7e7fVl0vDxPcCOlbH0eUadXIWaokwH7z3F"
  5 => array:9 [▼
    0 => "1"
    1 => "1"
    2 => "0"
    3 => "2"
    4 => "0"
    5 => "3"
    6 => "0"
    7 => "0"
    8 => "0"
  ]
  4 => array:9 [▼
    0 => "1"
    1 => "0"
    2 => "0"
    3 => "0"
    4 => "0"
    5 => "0"
    6 => "0"
    7 => "0"
    8 => "0"
  ]
  6 => array:9 [▶]
  "save" => null
]

where 4 =>, 5 =>, and 6 => represent user_ids and the arrays, their respective scores. I am not sure how I would go about iterating through the multiple arrays to store the data.

The DB table structure is id, user_id, game_id, col-1, col-2, etc

Hopefully, this is clear enough but if yo need more info, please let me know. Thanks

Here is the table code: This is the code I have for my table :

@foreach($players as $p)
                     <tr class="">
                        <td class="h2 align-middle">
                           {{ $players->get($loop->index)->first_name }}
                           <input type="text" name="user_id" value="{{ $p->id }}">
                        </td>
                        @foreach($innings as $inning)
                           <td class="p-1">
                              <div class="form-group_{{$p->id}} input-group p-1 h4">
                                 <select class="custom-select custom-select-lg py-0 prc" id="" name="player[{{$p->id}}]">
                                    <option class="" value="0">0</option>
                                    @foreach($scores as $score)
                                       <option name="score[]" value="{{ $score }}">{{ $score }}</option>
                                    @endforeach
                                 </select>
                              </div>
                           </td>
                        @endforeach
                        <td>
                           <div class="h1 text text-center font-weight-bold pt-2">
                              <output id="result_{{$p->id}}"></output>
                           </div>
                        </td>
                     </tr>
                  @endforeach

1 Answer 1

1
$data = [
    ['user_id'=>'1', 'game_id'=> 1, 'col-1'=>'some value', 'col-2'=>'other value'],
    ['user_id'=>'2', 'game_id'=> 1, 'col-1'=>'some new value', 'col-2'=>'other new value'],
    //...
];

Eloquent approach

Model::insert($data); 

Query Builder approach

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

Using the above method you can insert multiple entries in one query.

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

4 Comments

Thanks Ankit. I understand the concept, but how do I go from getting the data in the format I have to the format you specified? Is it in the way I am naming my fields? Or the structure of the form/table or something else?
You need to define the structure of array in the same way as your columns in table. You can easily define that by running it in for loop. And running an insert query in for loop takes more time than passing an array.
Apologies for not understanding your response. How would I define the structure? I have added my table code to the question.
Something like this you can do foreach($array as $val) { $data[] = ['user_id' => $val['user_id'], 'game_id'=>$val['game-id']] }

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.