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

Hello, I see what you are trying to do here. The reason your approach does not work is that the piece of javascript that is executed on submit will in fact run the submit() from all forms, but you are not sending it through any ajax request so the JS runs through all the submit quickly and the last form submit is the one that goes through all the way.

Here is another approach that you may try. Instead of sending multiple forms, you could send one form and use the [] syntax for the name attributes within your <input> tags. Here is an example:

<input type="text" name="person[0][first_name]" value="Clark">
<input type="text" name="person[0][last_name]" value="Kent">

<input type="text" name="person[1][first_name]" value="Bruce">
<input type="text" name="person[1][last_name]" value="Wayne">
...

This way, you wouldn't need the javascript code, nor would you need multiple <form>, just one. Your backend will have to change, as the data you receive will be put into an array, so a loop is in order.

public function store(Request $request)
{
    foreach($request->person as $person) {
        App\Test::create($person);
    }

   return redirect()->back();
}

Note that the above code is not safe, you will need to make sure that all fields are valid and that you do not have any mass assignment issues.

Hope this helps.

Last updated 5 years ago.
0
public function store(Request $request){
 $input = Input::all();
 $studentdata = $input['student_id'];
 $coursedata = $input['c_id'];
 $allOrder = array();

 foreach ($coursedata as $key => $val) {
    $do = new DO;
    $do->course_id = $input['c_id'][$key];
// others input

    foreach ($studentdata as $key => $val) {

       

//all input request data
     
    }
    $do->save();
 }
}

Let me know if you face any other problem thanks

Last updated 5 years ago.
0

Jeremy Michel, in your

  foreach($request->person as $person) {
          App\Test::create($person);
      }

When I create a person is there a way to pass in some default value in every person that is created. If yes can you please tell me. And BTW thanks for the reply it helped a lot.

0

Of course, I used the create() method in my example as a shorthand version. You could definitely do

foreach($request->person as $person) {
    $tests = new \App\Test([
        'first_name' => $person['first_name'],
        'last_name' => $person['last_name'],
        'foo' => 'bar' // Example of default value
    ]);
    $tests->whatever = "foo"; // Another example, basically, anything you want
    $tests->save();
}
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Sarthak Jha sjmonsta Joined 18 Jan 2019

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.