wim91's avatar

How to enter an unknown amount of data from a dynamic form into a database?

Hello everyone. I have a dynamic form that may contain an unknown number of fields. How can I best process this data in the controller and add it to the database?

My ideas: Iterate through the received data and add it to the database, but what's the best way to do this?

  1. Call the class instance each time in the loop: $add = new Mymodal;

  2. Use the Create method?

How would you solve this problem? Maybe there's a simpler way?

0 likes
9 replies
jlrdw's avatar

Does the form hold related data like an invoice would? You probably need a related table.

alex458's avatar

Hey wim91! 👋 Yeah, for a dynamic form like that, iterating through the submitted data usually makes sense. You don’t necessarily need to create a new instance each time in the loop—most frameworks let you pass an array of data to a create or insert method in one go, which is cleaner and more efficient.

So basically: collect the form data into an array, then either loop through it to insert each record or use a bulk insert if your framework/database supports it. Makes things simpler and faster.

wim91's avatar

What about the files? Should they also be converted into an array from $_FILE? I basically implemented this logic using two of my own classes, iterating through the Request received from the form and then iterating through the files.

Snapey's avatar

before saving the file, you probably need to save the data so that you can link them together in some way.

In the first instance I would just loop over the input, and optimise later if performance is an issue.

wim91's avatar

I'll try that. I'm worried there will be a problem with the files, since their number is initially unknown, and they, like the form data, may or may not be received. I initially tried iterating through the Request, but someone advised me to structure the data from the Request into its own array and work with that.

jlrdw's avatar

I usually limit files to a certain number at a time, I normally go 4 max. But that's just the way I handle it.

martinbean's avatar

@wim91 We’d be able to suggest better solutions if you told us what the actual problem you’re trying to solve is.

What is this form? Why is there an unknown amount of data?

baseciq's avatar

If your problem is simply deciding whether to use $model = new Model(); or Model::create(), the answer is simple: whichever is more convenient for you. Model::create() , according to Eloquent's excellent documentation, is simply a shortcut to new Model(). Model::create() will also return a model instance that you can operate on:

$model = Model::create(['name' => 'John', 'age' => 20]);
$model->name = 'Unfortunately not John';
$model->save();

The Eloquent documentation (https://laravel.com/docs/12.x/eloquent#inserting-and-updating-models) indicates that this is a one-line alternative and doesn't mention any significant differences:

Alternatively, you can use the create method to "save" a new model using a single PHP statement. The inserted model instance will be returned to you by the create method:

So when asked which method to use, use the one you like best.

If you want to operate on large numbers of rows and perform bulk inserts, you should skip Eloquent and work directly with the database (https://laravel.com/docs/12.x/queries#insert-statements). Just remember that none of Eloquent's mutators, modifiers, or other features will work in this case, as the following will build and execute the SQL query without creating any models:

DB::table('users')->insert([
['email' => '[email protected]', 'votes' => 0],
['email' => '[email protected]', 'votes' => 0],
]);

However, as others have pointed out, without showing the structure of the data being sent by the form and where you want to save it, it's hard to say if there are any other ways than iterating through it and either adding rows by row or preparing a batch for bulk insert.

Besides the above methods for adding data to the database, I don't know of any other methods for adding data to the database using Eloquent.

I hope this helps.

Please or to participate in this conversation.