I was wondering how do you access Input elements that have an id or name code for e.g. row_1_firstname and row_2_firstname - when you don't know exactly how may rows you have, how do you do batch inserts and how do you query the Input::get or Input::all() with undetermined number of rows for an insert and update procedure
public function store()
{
$array = Input::all();
foreach($array as $element) {
// do stuff in here
// store data in a array for batch insert and update processing
}
}
I would go for naming input as arrays firstname[]
and hidden inputs ids[]
with id to edit
public function store()
{
$names = Input::get('firstname');
$ids = Input::get('ids');
for ($i=0, $c = count($ids); $i<$c; ++$i) {
echo $names[$i].' was set for id '.$ids[$i];
}
}
EDIT
If you want to insert multiple records into array you can do it this way:
public function store()
{
$names = Input::get('firstname');
$surnames = Input::get('surname');
$data_array = [];
for ($i=0, $c = count($names); $i<$c; ++$i) {
$record = [];
$record['name'] = $names[$i];
$record['surname'] = $surnames[$i];
$data_array[] = $record;
}
DB::table('users')->insert($data_array);
}
Separating firstnames[]
and ids[]
and any other field you need is one way, but definitely not the most straightforward.
I suggest instead adding your inputs like:
// assuming you add users
users[1][firstname]
users[1][lastname]
users[2][firstname]
users[2][lastname]
...
of course users[1]
is controlled via js and it's dynamic users[i]
.
Then in your PHP code you can do simply this:
$users = Input::get('users');
// validate input according to your needs, then:
DB::table('users')->insert($users);