2016-06-04 3 views
0

Ich habe Pivot-Tabellen item_color und item_size und möchte meine Größe und Farbfelder mithilfe ihrer Feldwerte aktualisieren und nicht genau, wo ich anfangen soll. Hier ist, was ich bisher habe.Aktualisieren von Objekt aus Pivot-Tabellenfelder

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

Ansicht

<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> 
+0

Was meinst du damit "meine Größe und Farbfelder mit ihren Feldwerten"? Was genau sind meine Felder und ihre Felder? Bitte sei klar. – crabbly

+0

Das Feld 'color' in der Pivottabelle item_color. https://i.imgur.com/7b55s7u.png und das Feld Größe in der Tabelle item_size https://i.imgur.com/j3nUci0.png –

Antwort

0

item_size und item_color sind keine Pivot-Tabellen. Sie sind in der Tat eine Eins-zu-eins-Beziehung zu Ihrem Schema. Wenn Sie wirklich Pivot-Tabellen möchten, müssen Sie eine Farb- und eine Größentabelle definieren und anschließend eine Viele-zu-Viele-Beziehung erstellen. Siehe hier: https://laravel.com/docs/master/eloquent-relationships

Um verwandte Modelle zu aktualisieren, verwenden Sie dann die Methoden attach, sync, detach, etc.

Auch, welche Version von Laravel laufen Sie? Wenn Sie 5.1+ ausführen, sehen Sie sich die Validierung der Formularanfrage an. Siehe hier: https://laravel.com/docs/master/validation#form-request-validation

Dadurch wird die gesamte Validierungslogik von Ihrem Controller entfernt.

+0

Ein 'wahrer' Pivot ist vielleicht nicht das, was ich brauche. Ich möchte nur viele Farben für viele Gegenstände und viele Größen für viele Gegenstände speichern, wie auch immer ich kann. –

+0

Ich benutze Laravel 4 –

+0

Sounds wie Pivot-Tabellen sind genau das, was Sie in diesem Fall brauchen. Sie beschreiben eine klassische Viele-zu-Viele-Beziehung. Siehe hier: https://laravel.com/docs/4.2/eloquent#many-to-many – btl

Verwandte Themen