0

これが私の質問の説明です。

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!

4

3 に答える 3

0

このコードを繰り返す

<?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
于 2013-07-05T19:51:37.377 に答える
0

ingrediens という別のテーブルを作成できます。

ID | 値 | prod_id

そして、そこに各製品のすべての材料を保管できます。次に、データを挿入するときに、 foreach を使用してそれらをループに挿入できます

役に立つことを願っています

于 2013-07-05T19:38:53.463 に答える
0

これに対する唯一の解決策は、ちょっとしたマニュアルだと思います...

最も基本的な例を見てみましょう...

<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を設定する必要があります...もっと良い方法が必要ですか?

于 2015-02-19T05:07:28.583 に答える