ol
4 months ago
741
4
Laravel

best way to save an array in a db

Posted 4 months ago by ol

Hello! Tell me what is the best way to write to a database from a form with several dynamical fields? My option throws an error. Hello! Tell me what is the best way to write to a database from a form with several dynamical fields? My option throws an error There is a form with several constant fields, similar dynamic fields are added to it - there can be up to 100 pieces. In the database, you need to save the line as: the value of constant fields plus the value of dynamic fields. abbreviated code: the form:

<form><
	<table>
		<tr>
			<td><input name='datestamp[]'/><input name='numbe[]r'/><input name='name'[]/>
		</tr>
		<tr>
			<td><input name='prod[]'/><input name='artikul[]'/><input name='price[]'/><button>Add row</button>//dinamycs fields
		</tr>
	</table
<button type="submit'>Send</button>
</form>

Controller:

 public function insert(Request $request){
	$datestamp= $request->input('datestamp');
	$number= $request->input('number');
	$name= $request->input('name');
	$prod= $request->input('prod');
	$artikul= $request->input('artikul');
	$price= $request->price('artikul');
foreach ($request as $user){
	DB::insert('insert into supplys (datestamp,number,name,prod,artikult,price)
 values(?,?,?,?,?,?,?,?,?,?,?)', [$datestamp,$number,$name,$prod,$artikul,$price])
};
return redirect () -> route ( 'new' );
}

thank you for any help!

Please sign in or create an account to participate in this conversation.