I am working on Laravel. I have a blade view page on which multiple forms (of same model) are created. Now, I want, that when I click submit button, an array of all the forms should be returned to controller. But, it returns data of only one form instead of array. How can I achieve this, Can anyone help me?
-
You can't send multiple form aaik, can't you just merge all the fields? – Damien Pirsy Jan 26 '16 at 10:07
-
How can I merge them? – Black_Swat Jan 26 '16 at 10:10
-
Just put all the fields within the same `` tags... If you want some kind of separation (logical, semantical, etc) you can achieve that easily with ` – Damien Pirsy Jan 26 '16 at 10:14
-
you have to use all fields in a single form – Imtiaz Pabel Jan 26 '16 at 10:15
-
Yes, I have all those forms in another single form. But, when I submit them, blade returns the data of only the last form among from multiple. – Black_Swat Jan 26 '16 at 10:33
4 Answers
It's not a real Laravel issue.
The trick is using different forms with each a submit button. Then, it's simple. Check the submitted button.
if ($request->isMethod('post')) {
if ($request->has('submit_button_form_1')) {
// Handle form
}
elseif ($request->has('submit_button_form_2')) {
// Handle form
}
elseif ($request->has('submit_button_form_3')) {
// Handle form
}
}

- 5,726
- 2
- 28
- 56
-
thanks @Schellingerht for the code. But, I have put all those multiple forms in one single form. You can say that now, all forms are the child of that single form. – Black_Swat Jan 26 '16 at 10:37
-
Subforms are not valid html. See also http://stackoverflow.com/questions/379610/can-you-nest-html-forms. It's better to split, so you can handle the different forms. – schellingerht Jan 26 '16 at 10:42
What you can do is you need to loop the form field with naming structure as first_name1, first_name2 and so on. Then, after submitting the form, you can validate using the loop in the same way and get the values. You can check my sample code here. It count the number of loops to be made.
You can pass the counter from the view to the controller in hidden field. Then, you can loop for validation and for taking the inputs.
Check my sample code:
//determine number of rows in database
$number_of_loop = Input::get('number_of_loop');
$arrayList = []; //defining the array variable
//declaring the validations rules
for($i = 1; $i < $number_of_loop; $i++){
$rules = array(
'currency'.$i => 'required',
'iso_code'.$i => 'required',
'symbol'.$i => 'required',
'status'.$i => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if($validator->fails()){
return Redirect::back()->withErrors($validator)->withInput();
}
}
for ($i=1; $i <= $number_of_loop; $i++) {
$new_currency = new stdClass();
$currency = Input::get('currency' . $i);
$iso_code = Input::get('iso_code'.$i);
$symbol = Input::get('symbol'.$i);
$conversion_rate = Input::get('conversion_rate'.$i);
$status = Input::get('status'.$i);
$new_currency->currency = $currency;
$new_currency->iso_code = $iso_code;
$new_currency->symbol = $symbol;
$new_currency->conversion_rate = $conversion_rate;
$new_currency->enable = $status;
array_push($arrayList, $new_currency);
}
Update1
For storing all the records, create an array and a new stdclass. Store all the variable in stdclass attributes. and at last, push the new stdclass to the array. Then, you can access all the inputs of form. Check above updated code sample

- 953
- 1
- 13
- 38
-
thanks for the code. But it's not working in my case. If I do the same, I got the data of only the last form among from multiple. – Black_Swat Jan 26 '16 at 10:35
-
You need to save data in each loop. Otherwise, you will get the values from the last loop only. First save the records in each loop and access from database. – VijayRana Jan 26 '16 at 10:47
-
sorry I can't explained it better. Suppose, When I run the loop, say 4 times, each iteration has the data of last form. I had tested it using echo. – Black_Swat Jan 26 '16 at 10:51
First of all I dont think you can send multiple forms, that is just way it is. You have two options.
First: Make one form with all fields from all forms, and then in controller you just take what field you want, just like you would do from arrays so same thing. Like:
$all_fields= Request::all();
and then
$all_fields['something'], $all_fields['something_else'],
Second: Using ajax, you can manipulate and send values of fields, fields that you need.

- 175
- 6
-
thanks @MasterSith. But I can't just put all fields in single form as those multiple forms are generating dynamically on button click. When user want to insert some more data, he just click the button and new form got appended under the already present ones. Then on submit click of parent form, I want the data of all child forms to be returned as array. But I got your point that we can't send multiple forms. Thanks for that. I will try some other way for it. – Black_Swat Jan 26 '16 at 10:42
-
Use ajax then, it will take values from inputs and send them to controller, u can make arrays, or what ever you like. – MasterSith Jan 26 '16 at 11:56
If you’re using Laravel 5.2 then you can validate array data. You’ll need to put all of your inputs in one form as you just can’t send multiple forms. If each form had different URLs for the action
attribute, how would that work?
To send data as an array, you can suffix the name
attribute with []
:
{!! Form::open() !!}
{!! Form::text('name[]') !!}
{!! Form::text('name[]') !!}
{!! Form::text('name[]') !!}
{!! Form::close() !!}
When this form is submitted, name
will be an array with three elements.
To validate, use a wildcard in your validation rule name:
return [
'name.*' => 'required|max:255',
];
You can then access the array of names in your controller through the Request
object:
$names = $request->get('names', []);
foreach ($names as $name) {
User::create(['name' => $name]);
}
I don’t know your data structure as you haven’t included it in your question, but this should be enough for you to apply to your given scenario.

- 38,379
- 25
- 128
- 201