Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

How to store multiple identical select fields ?

Hello,

I'm working on a personal project : an app to save recipes. I have two tables : recipes and ingredients, one recipe have many ingredients.

I want to have only one form with the fields for the recipe and the fields for the ingredients. So I have a button to add a new ingredient (jQuery, clone of a row containing three fields : quantity, unit, product name in the form). The product name is a select field. There will be one, two, three or more ingredients, and then one, two, three or more quantity fields, unit fields and product name fields.

Is it possible to retrieve all fields through a simple way ? The perfect case would be an array with the ingredients. How is it possible ?

Thanks a lot for your help.

Vincent

0 likes
6 replies
martbock's avatar

Sorry, but I don't quite get how your data model works. Would you mind posting a quick visualization of your model tables and their fields?

Is this what you have in mind?

Recipe:
- text?

Ingredients:
- recipe_id
- quantity
- unit
- product_name

What do you mean with retrieving all fields? If you use Eloquent, you could do something like $recipe->ingredients if you set up the corresponding relation.

vincent15000's avatar

Sure I can. Here it is.

Recipe
- description
- portions_number
- preparation_time
- standing_time
- cooking_time

Product
- name

Ingredient
- recipe_id
- product_id
- quantity
- unit_id

There is no problem to show the recipe and its ingredients.

The problem is to create a new recipe with all its ingredients with one unique form.

martbock's avatar

Aha – okay. So what you could do is to send a POST request with a body like:

{
	'description': 'foo',
	...,
	'ingredients': [
		{ 'product_id': 42, 'quantity': 1, 'unit_id': 'g' }
	]
}

In your controller, you can validate each ingredient with something like:

[
	'ingredients' => 'array|...',
	'ingredients.*.product_id' => 'distinct|exists:products|...',
	...
]

Then, you would then create the recipe and iterate over each ingredient and create an entity for each one. All of this should probably happen within a database transaction.

1 like
vincent15000's avatar

Yes that's it. Here is a screen capture of the form.

https://drive.google.com/file/d/1t8clBrBgtjWSzvwd7mfam7NI5ZYjNxeD/view?usp=sharing

Doing this with a database transaction is not a problem for me. What I don't know how to do is the form. Do I have to retrieve the data with jQuery to make an array or is there a simpler way ?

Well perhaps a better question : how can I do my form in order to be able to retrieve an array of ingredients ?

martbock's avatar
martbock
Best Answer
Level 6

Got you. You can give your inputs a name like ingredients[0][product_id] with the 0 being a counter value. So the following HTML should work without the need for an AJAX request.

<form ...>
	<input name="ingredients[0][product_id]" value="42">
	...
	<input name="ingredients[1][product_id]" value="13">
	...
</form>

When you add a new ingredient with jQuery, you would increase the counter value for the new field row.

1 like
vincent15000's avatar

Oh thank you very much. I didn't know it was possible. That's exactly what I need. ;)

1 like

Please or to participate in this conversation.