これが私の質問の説明です。
I have usual form and it is not clear to me how with one function and one form to enter multiple rows in the same database table with same form inputs because in my case I have the ingredients of products and every ingredients should be new row in database. The only solution I know is to make add one ingredient at a time but it is not a good solution... I might know how to solve it with classical php functions but I do not know with laravel functions, is probably easier with laravel.
This is function:
public function post_add()
{
if(Input::get('submit'))
{
$input = Input::all();
$validation = Validator::make($input);
if($validation->fails())
{
Vsession::cadd('r', $validation->errors->first())->cflash('status');
}
else
{
$items['product_weight'] = Input::get('product_weight');
$items['product_code'] = Input::get('product_code');
$items['raw_material'] = Input::get('raw_material');
foreach ($items as $key => $value)
{
$items[$key] = ($value !== '') ? trim(filter_var($value, FILTER_SANITIZE_STRING)) : null;
}
try
{
$id = DB::table('products_ingredients')->insert_get_id(array(
'product_weight' => $items['product_weight'],
'product_code' => $items['product_code'],
'raw_material' => $items['raw_material'],
));
}
catch(Exception $e)
{
Vsession::cadd('r', __('error'))->cflash('status');
return Redirect::to_action('products@add');
}
Vsession::cadd('g', __('error'))->cflash('status');
return Redirect::to_action('products@add');
}
}
return $this->get_add();
}
and add.php
<?php
echo Form::text('product_weight', Input::get('product_weight'));
echo Form::text('product_code', Input::get('product_code'));
echo Form::text('raw_material', Input::get('raw_material'));
?>
But I need to repeat inputs several times, like this:
<?php
echo Form::text('product_weight', Input::get('product_weight'));
echo Form::text('product_code', Input::get('product_code'));
echo Form::text('raw_material', Input::get('raw_material'));
echo Form::text('product_weight', Input::get('product_weight'));
echo Form::text('product_code', Input::get('product_code'));
echo Form::text('raw_material', Input::get('raw_material'));
....
?>
I hope I managed to explain. Thanks in advance!