从数据透视表字段更新项目

时间:2016-06-04 14:49:05

标签: laravel laravel-4

我有item_color和item_size数据透视表,并希望使用其字段值更新我的大小和颜色字段,并且不确定从何处开始。这是我到目前为止所拥有的。

ItemSize.php

<?php

class ItemSize extends \Eloquent
{
    protected $table = 'item_size';
    protected $fillable = [];

    public function item() {
        return $this->belongsTo('Item');
    }
}

ItemColor.php

<?php

class ItemColor extends \Eloquent
{
    protected $table = 'item_color';
    protected $fillable = [];

    public function item() {
        return $this->belongsTo('Item');
    }
}

VendorController

public function postVendorUpdateItems ($id)
         {

             $input     = Input::all();

             $items = Item::find($id);
             $validator = Validator::make($input, 
               [ 'item_name'     => 'max:50',
                 'item_id'       => 'max:50',
                 'normalprice'   => 'numeric',
                 'karmaprice'    => 'numeric',
                 'asin'          => 'max:50',
               ]);

             if($validator->passes())
             {

                 $items->name           = $input['item_name'];
                 $items->normalprice    = $input['normalprice'];
                 $items->karmaprice     = $input['karmaprice'];
                 $items->asin           = $input['asin'];
                 $items->id             = $input['item_id'];
                 $items->save();
                 return Redirect::route('account-vendor-index')
                                ->with('global', 'You have updated your item.');
             }

             return Redirect::route('account-vendor-index')
                            ->withErrors($validator)
                            ->with('global', 'Your item could not be updated.');
         }

查看

<form class="form-horizontal" role="form" method="post" action="{{url('account/vendor/update/items')}}/{{$item->id}}">
                <input type="hidden" id="brand_id" placeholder="brand_id" value="{{$brand->id}}" name="brand_id">
                <input type="hidden" id="item_id" placeholder="item_id" value="{{$item->id}}" name="item_id">

                <div class="form-group">
                  <label for="colors" class="col-xs-3 control-label">Colors</label>
                  <div class="col-xs-6">
                    <input type="text" class="form-control input-sm" id="colors" name="colors" placeholder="@foreach($item->colors as $color){{$color->color}}@endforeach" value="">
                  </div>
                  <button type="" class="btn btn-primary btn-sm">Add color</button>
                  <div class="clear"></div>
                  <div class="col-xs-offset-3 showColors">
                    @foreach($item->colors as $color)
                      {{$color->color}}
                    @endforeach
                  </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-3 control-label">Sizes</label>
                    <div class="col-xs-9">
                        <select id="selectSizes" multiple="multiple" class="form-control selectSizes" name="sizes">
                            <option value="XS (0-2)">XS (0-2)</option>
                            <option value="S (4-6)">S (4-6)</option>
                            <option value="M (8-10)">M (8-10)</option>
                            <option value="L (12-14)">L (12-14)</option>
                            <option value="XL (16-18)">XL (16-18)</option>
                            <option value="XXL (20-22)">XXL (20-22)</option>
                        </select>
                    </div>
                </div>
                <div class="form-group bot-0">
                  <div class="col-xs-offset-3 col-xs-9">
                    <button type="submit" class="btn btn-main btn-sm">Save changes</button>
                  </div>
                </div>
            </form>

1 个答案:

答案 0 :(得分:0)

item_size和item_color不是数据透视表。事实上,它们与您的架构是一对一的关系。如果您真的想要透镜表,则需要定义颜色和尺寸表,然后创建多对多关系。见这里:https://laravel.com/docs/master/eloquent-relationships

然后要更新相关模型,您将使用附加,同步,分离等方法。

另外,你在运行什么版本的Laravel?如果您正在运行5.1+,请查看表单请求验证。见这里:https://laravel.com/docs/master/validation#form-request-validation

这将从控制器中删除所有验证逻辑。