如何用一个函数和一个表单在数据库中输入多行相同的输入-Laravel


How with one function and one form to enter multiple rows in DB with same inputs - Laravel

这是对我的问题的解释。

我有通常的表格,我不清楚如何用一个函数和一个表格在同一个数据库表中用相同的表格输入多行,因为在我的情况下,我有产品的成分,每个成分都应该是数据库中的新行。我知道的唯一解决方案是一次添加一种成分,但这不是一个好的解决方案。。。我可能知道如何用经典的php函数来解决它,但我不知道用laravel函数,用laraver可能更容易。

这是功能:

  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();
}

并添加.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'));
?>

但我需要重复输入几次,比如:

    <?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'));
....
?>

我希望我能解释清楚。提前感谢!

您可以制作另一个名为ingrediens:的表

ID|value|prod_ID

在那里你可以储存每种产品的所有成分。然后,当插入数据时,您可以使用foreach并将它们插入循环

希望它是有用的

重复此代码

<?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'));
?>

在一个循环中预成型,它应该可以与你的一起工作


我不擅长laravel,但这里有一个循环的例子

@for ($i = 0; $i < 10; $i++)
    The current value is {{ $i }}
@endfor
@foreach ($users as $user)
    <p>This is user {{ $user->id }}</p>
@endforeach
@while (true)
    <p>I'm looping forever.</p>
@endwhile

我认为唯一的解决方案是手动。。。

举一个最基本的例子。。。

<div class="name">
    {{ Form::text('name[]') }}
</div>

如果您使用一些javascript,您可以根据需要多次复制整个DIV(使用类似$('name').clone()的东西)

你可以在你的PHP中用。。。

$names = Input::get('name');
foreach($names as $name){
    //do whatever you want
}

最初的问题是如何使用多个字段(即姓名和年龄)来实现这一点。我能想到的唯一答案是这样的

<div class="person">
    {{ Form::text('persons[ID][name]') }}
    {{ Form::text('persons[ID][age]') }}
</div>

但每次复制行时都必须设置ID。。。必须有更好的方法吗?