BeardedInBinary
BeardedInBinary

Reputation: 725

Repeating Form Fields In Laravel

I have a form which is submitting data to 2 separate models.

The model SalesItem belongs to Sale. A Sale can have many SalesItems.

I know I need to get the repeating fields into an array, but I'm not entirely sure how I should go about doing that. I though I had the right idea, but my controller isn't catching the array as the variable I was trying to set it up as.

Can see relevant code here: http://laravel.io/bin/Qzk24

All help is greatly appreciated!

/*** 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
****/

Upvotes: 0

Views: 3747

Answers (1)

VF_
VF_

Reputation: 2653

Well you have a few errors in your code, but I'll try to give you a direction..

Replace the input fields with something like this:

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

Grab the items using $items = Input::get('items'); (btw this is the point where php threw an error: you never got the $items from the request)

Iterate over the items array and add every single one of it to your Sale model..

Upvotes: 1

Related Questions