在Laravel中重复表单字段

时间:2014-04-25 21:47:26

标签: php laravel

我有一个表单,它将数据提交给2个单独的模型。

SalesItem模型属于Sale。销售可以有许多SalesItems。

我知道我需要将重复的字段放入数组中,但我并不完全确定应该如何去做。虽然我有正确的想法,但我的控制器并没有抓住数组作为我试图将其设置为的变量。

可在此处查看相关代码:http://laravel.io/bin/Qzk24

非常感谢所有帮助!

/*** Sale Model  ***/
    class Sale extends \Eloquent {
    public function user(){
        return $this->belongsTo('User');
    }

    public function salesItem()
    {
        return $this->hasMany('SalesItem');
    }

    }

/*** SalesItem Model ***/
    class SalesItem extends \Eloquent {
        protected $table = 'SalesItems';

    public function sale()
    {
        return $this->belongsTo('Sale');
    }
    }

/**** SalesController@store ****/
    public function store()
    {
        $sale = new Sale();
        if(Auth::user()){
            $userID = Auth::user()->id;
            $sale->user_id = $userID;
        }
        $sale->invoice_number = Input::get('invoice_number');
        $sale->name_of_purchaser = Input::get('name_of_purchaser');
        $sale->date_of_sale = Input::get('date_of_sale');


        // For each saleItem that exists
        foreach($items as $item)
        {
            //Create new salesitem
            $saleItem = new SalesItem();
            //Save attributes 
            $saleItem->product_id = $item['equipment'];
            $saleItem->selling_price = $item['selling_price'];
            $saleItem->serial_number = $item['serial_number'];
            $saleItem->save();

            //associate with the sale
            $sale->salesItem()->associate($salesItem);
        }
        $sale->save();

        return "Sale Saved";
    }

/**** sales/create.blade.php *****/
     {{ Form::open(array('action'=>'SalesController@store')) }}
    <ul>
    <li>
        {{ Form::label('invoice_number','Dealer Invoice Number:')}}
        {{ Form::text('invoice_number')}}
        {{ $errors->first('invoice_number','<small class="error">:message</small>')}}
    </li>
    <li>
        {{ Form::label('name_of_purchaser','Name of Purchaser:')}}
        {{ Form::text('name_of_purchaser')}}
        {{ $errors->first('name_of_purchaser','<small class="error">:message</small>')}}
    </li>

    <li>
        {{ Form::label('date_of_sale','Date of Sale:')}}
        {{ Form::text('date_of_sale')}}
        {{ $errors->first('date_of_sale','<small class="error">:message</small>')}}
    </li>

    <li>
        <h2> Sale Items </h2>
            <ul class="repeatable"> 

                <li>
                    {{ Form::label('items[0][equipment]','Equipment:')}}
                    {{ Form::text('equipment[0]', Input::get('equipment'), array('class' => 'form-control clone'))}}
                    {{ $errors->first('equipment','<small class="error">:message</small>')}}
                </li>

                <li>
                    {{ Form::label('selling_price[0]','Selling Price:')}}
                    {{ Form::text('selling_price[0]', Input::get('selling_price'), array('class'=>'form-control clone'))}}
                    {{ $errors->first('selling_price','<small class="error">:message</small>')}}
                </li>

                <li>
                    {{ Form::label('serial_number[0]','Serial Number:')}}
                    {{ Form::text('serial_number[0]', Input::get('serial_number'), array('class'=>'form-control clone'))}}
                    {{ $errors->first('serial_number','<small class="error">:message</small>')}}
                </li>
                <li>
                    <button href="#" class="add" rel=".clone"><i class="fa fa-plus-square"></i> Add Item</button>
                </li>
            </ul>
    </li>
    <li>
        {{ Form::submit($buttonText) }}
    </li>
    </ul>
    {{ Form::close(); }}


/**** Getting this error Undefined variable: items 
 * /app/controllers/SalesController.php
****/

1 个答案:

答案 0 :(得分:1)

你的代码中有一些错误,但我会尝试给你一个方向..

用以下内容替换输入字段:

{{ Form::text('items[0]["equipment"]', array('class' => 'form-control clone'))}} 
.... 
{{ Form::text('items[0]["selling_price"]', array('class' => 'form-control clone'))}}
....

使用$items = Input::get('items');抓取项目(顺便说一下,这是php发出错误的一点:你从来没有从请求中获得$ items)

迭代items数组并将其中的每一个添加到Sale模型中。