Support the ongoing development of Laravel.io →
Database Eloquent Forms
Last updated 1 year ago.
0

I dont really get this line : Item Qty Test 1 1 Test 2 2 Test 3 3

Last updated 1 year ago.
0

I think its just a for each and process the form

Last updated 1 year ago.
0
 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

Last updated 1 year ago.
0

Yep like above is it possible to show a example to process the form.

Would you use

$data=Input::all(); and loop thru that?

Last updated 1 year ago.
0

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));
}
Last updated 1 year ago.
0

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

Last updated 1 year ago.
0

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

Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0
$data = array(
    array('name'=>'Bar', 'lastname'=>'Baz'),
    array('name'=>'Foo', 'lastname'=>'Baz'),
);

YourModel::insert($data);
Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

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") ) 
Last updated 1 year ago.
0

I figured it out.

Last updated 1 year ago.
0

Could you please share what your final result was?

Last updated 1 year ago.
0

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 );
Last updated 1 year ago.
0

can i have the code logic??

Last updated 1 year ago.
0

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[]

Last updated 9 years ago.
0

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

0

Sign in to participate in this thread!

Eventy

Your banner here too?

kyoukhana kyoukhana Joined 28 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.