I am using the simple form and array input like this (not dynamic)
<input type="text" name="pre_ref_position[]" id="pre_ref_position">
<input type="text" name="pre_ref_position[]" id="pre_ref_position">
Form post with ajax and return data like this;
[
0:{pre_ref_position:'example1'}
1:{pre_ref_position:'example2'}
]
So I want to save this data with eloquent but how can I handle this array and save each row in the database?
Assuming you receive your array in a variable called $refVal
in your controller
Then use the following code:
$refArr = [];
foreach($refVal as $key => $val)
{
$now = Carbon::now();
//ref_pos in the array below refers to the field name in your database.
$refArr[] = [ 'ref_pos' => $val['pre_ref_position'] ,'created_at' => $now, 'updated_at' => $now ];
}
//assuming model name is `Position`
Position::insert($refArr);