I dont really get this line : Item Qty Test 1 1 Test 2 2 Test 3 3
I think its just a for each and process the form
Item Qty
.............................
Test 1 1
Test 2 2
Test 3 3
hmmm, like above ?
yes,,,you can use Loop put them in Array and Insert Batch
Yep like above is it possible to show a example to process the form.
Would you use
$data=Input::all(); and loop thru that?
A couple things to make sure you get this working
First.. make sure to add square brackets to the name of the text input. This will make sure that even if only one is submitted that an array will be always be returned.
<input type="text" name="qty[]" value="1" />
<input type="text" name="qty[]" value="2" />
<input type="text" name="qty[]" value="3" />
<input type="text" name="qty[]" value="4" />
Then you want to get the array of items and loop through them.
$quantities = Input::get('qty');
foreach($quantities as $quan) {
DB::insert('insert into EXAMPLE (quantity) values (?)', array($quan));
}
I was thinking something like this. But you might not be able to use Laravel eloquent What i was thinking was something like this
$data=Input::all();
foreach ($data as $key => $value) {
foreach ($data[$key] as $v) {
/*Call insert database */
$recrd = new dbrecord();
/*Then you would map the keys with if statements */
$recrd->recordID=''
}
}
But mapping the keys would be a lot of if statements or case select
Looping trough it one by one and insert it to DB is a bit disaster...
You'd better loop trough it and save in b array variable...then INSERT BATCH it to DB
You are right its a disaster. So do you have a example of what kind of array it should be to perform this. Because I am using eloquent. How would you do a batch insert.
$data = array(
array('name'=>'Bar', 'lastname'=>'Baz'),
array('name'=>'Foo', 'lastname'=>'Baz'),
);
YourModel::insert($data);
Just confused here how would I loop using a for each and create that array above. So lets say the input data is this
<input type="text" name="name[]" value="Foo" />
<input type="text" name="name[]" value="Bar" />
<input type="text" name="lastname[]" value="Baz" />
<input type="text" name="lastname[]" value="Bax" />
Then the php code
$lastname = Input::get('lastname');
$name = Input::get('name');
foreach($lastname as $key => $n ) {
/*Load array */
}
Just Confused on loading the array so it looks like the example you have provided above.
In the for each loop i have this
foreach($lastname as $key => $n ) {
/*Load array */
$arrData[] = array("lastname"=>$lastname[$key], "name"=>$name[$key]);
}
but when i print the array it looks like this and nothing like geocine example array
Array ( [0] => Array ( [lastname] => "baz" [name] => "foo" ) [1] => Array ( [lastname] => "bax" [name] => "bar") )
Could you please share what your final result was?
I am doing it this way, but not sure this is the best way...
$item_id = Input::get('item_id');
$user_id = Input::get('user_id');
foreach($item_id as $key => $n )
{
$item = Item::find( $item_id[$key] );
$arrData[] = array(
"order_id" => Input::get('order_id'),
"user_id" => $user_id[$key],
"item_id" => $item_id[$key],
"item_price" => $item->price,
"item_currency" => $item->currency,
"quantity" => 1
);
}
Model::create( $arrData );
If you have an error in your form and return withInput, this method trows errors because laravel cant put the individual items back in the text inputs. It tries to put the array in the first text field it finds and throws an error because the expected value is a string not an array.
At least thats is in my experience, maybe someone knows a way around this?
edit nvm, just use qty[0], qty[1] instead of qty[]
How would I post that type of array to a database without the values? Can I just send the user input to the database to an array with comma seperations
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community