简体   繁体   中英

Dynamic form element names in Blade for Laravel 5.2

I'm attempting to create a form to update multiple rows of data from a table. The table has four columns ( cola, colb, colc, cold ). I am generating the form dynamically with a foreach loop in my view. At present I am using

text('cola[][cola]', $this->cola...

And on down to name and then populate the form fields. This works fine except that it returns an array of four arrays ( cola, colb, colc, cold ), so that I have all of my cola values in one array and all of my colb values in another etc.

What I would like is to return an array of each row that is submitted, so that my result would be something like

0(cola=3, colb=7, colc=2, cold=99)

So that I can access the values simply with a for each loop. I am, however, HTML remedial and cannot seem to get the name right on the form elements to accomplish this. I know that the answer is obvious, but I keep running into either the arrays I do not want or only submitting the data from the final row.

Edit for clarifications...

Use of

text('row[$this->key][cola]'

Or

text('row[$i][cola]'

with $i as an iterator results in one array with the name of $i or $this->key that only returns the last row submitted. Removing the quotes, as in

text(row[$i][cola]

Results in an undefined constant, because the string is expected.

The closest that I have come to success is

text('row['<php echo $i ?>'][cola]'

This actually names the form elements correctly, but breaks the elements themselves. They render as plain text and not as input boxes. I really am going a bit bonkers on this one.

'' is for string literals, you want interpolation, "" .

$a = 'hi';
echo "$a there";
// hi there

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM