2016-04-07 1 views
0

Wie würde ich meine Daten in meiner Auswahlbox aufbewahren, wenn ich zurück gehe und ein Produkt bearbeite?Abrufen von Daten im Dropdown-Listenfeld beim Bearbeiten von Formularen - Laravel 5.2

Hier ist meine Form mit Eltern und Unterkategorien:

  <form role="form" method="POST" action="{{ route('admin.product.update', $product->id) }}"> 
       {{ csrf_field() }} 


       <div class="col-xs-12 col-sm-6 col-md-6"> 
        <div class="form-group{{ $errors->has('category') ? ' has-error' : '' }}"> 
         <label>Parent Category</label> 
         <select class="form-control" name="category" id="category" data-url="{{ url('api/dropdown')}}"> 
          <option value=""></option> 
          @foreach($categories as $category) 
           <option value="{{ $category->id }}">{{ $category->category }}</option> 
          @endforeach 
         </select> 
         @if($errors->has('category')) 
          <span class="help-block">{{ $errors->first('category') }}</span> 
         @endif 
        </div> 
        <br> 
       </div> 

       <div class="col-xs-12 col-sm-6 col-md-6"> 
        <div class="form-group{{ $errors->has('cat_id') ? ' has-error' : '' }}"> 
         <label>Sub-Category Category</label> 
         <select class="form-control" name="cat_id" id="sub_category"> 
          <option value=""></option> 
         </select> 
         @if($errors->has('cat_id')) 
          <span class="help-block">{{ $errors->first('cat_id') }}</span> 
         @endif 
        </div> 
        <br> 
       </div> 


       <div class="form-group col-md-12"> 
        <button type="submit" class="btn btn-primary waves-effect waves-light">Edit Product</button> 
       </div> 

      </form> 

Hier ist meine Funktion ist die Ergebnisse bearbeiten Form zu erhalten:

/** 
    * Return the view to edit & Update the Products 
    * 
    * @param $id 
    * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View 
    */ 
    public function editProduct($id) { 

     // Find the product ID 
     $product = Product::findOrFail($id); 

     // Get all Categories where parent_id = NULL 
     $categories = Category::whereNull('parent_id')->get(); 


     // Return view with products and categories 
     return view('admin.product.edit', compact('product', 'categories')); 

    } 

Hier meine Kategorie Model für meine Kategorien und Unterkategorien:

class Category extends Model { 

    protected $table = 'categories'; 

    protected $fillable = ['category']; 


    /** 
    * One sub category, belongs to a Main Category (Or Parent Category). 
    * 
    * @return \Illuminate\Database\Eloquent\Relations\BelongsTo 
    */ 
    public function parent() { 
     return $this->belongsTo('App\Category', 'parent_id'); 
    } 


    /** 
    * A Parent Category has many sub categories 
    * 
    * @return \Illuminate\Database\Eloquent\Relations\HasMany 
    */ 
    public function children() { 
     return $this->hasMany('App\Category', 'parent_id'); 
    } 


    /** 
    * One Category can have many Products. 
    * 
    * @return \Illuminate\Database\Eloquent\Relations\HasMany 
    */ 
    public function product() { 
     return $this->hasMany('App\Product', 'id'); 
    } 

} 

Hier ist mein Produkt-Modell für meine Produkte Tabelle:

class Product extends Model { 

    protected $table = 'products'; 

    protected $fillable = [ 
     'product_name', 
     'price', 
     'cat_id', 
     'featured', 
     'brand_id', 
    ]; 


    /** 
    * One Product can have one Category. 
    * 
    * @return \Illuminate\Database\Eloquent\Relations\HasOne 
    */ 
    public function category() { 
     return $this->hasOne('App\Category', 'id'); 
    } 


} 

Und hier ist, wie meine Kategorien und Produkte Tisch eingerichtet ist: My Categories Table

My Products table

Nur damit Weißt du, wenn ich eine Elternkategorie auswähle, feuert ein Ajax-Aufruf ab und ruft alle übergeordneten Unterkategorien ab. Ich habe das in dieser Frage nicht aufgenommen.

Antwort

1

Für Creation Formen:

@foreach($categories as $category) 
    <option value="{{ $category->id }}" @if(old('category')&&old('category')== $category->id) selected='selected' @endif >{{ $category->category }}</option> 
@endforeach 

Für bearbeiten Formen:

@foreach($categories as $category) 
    <option value="{{ $category->id }}" @if($category->id==$model->category) selected='selected' @endif >{{ $category->category }}</option> 
@endforeach   
+0

Ich habe Probleme mit $ -Modell, kann nicht herausfinden, was dort geht, die obere funktioniert zwar, aber nicht die Bearbeitung – David

+0

Das ist nur die Variable mit der Option Ergebnis, in der Regel ich w ould verwenden ein Modell für diese – LewiG

0

Zuerst stellen Sie sicher, Ihre Eingabe umgeleitet wird zurück zum Formular:

return redirect('form')->withInput(); 

dann den Schlüssel überprüfen mit Ihre alten Eingabedaten:

@foreach($categories as $category) 
    <option value="{{ $category->id }}" {{ ($category->id == old('category') ? 'selected="selected"' : '') }}>{{ $category->category }}</option> 
@endforeach 
0

Dies ist eine Erklärung zu @ LewiG's Antwort.

Beim Erstellen eines Elements geben wir die Eingabe zurück, wenn sie ungültig ist. In diesem Fall sollten wir unsere Ansichten so vorbereiten, dass sie den old($key)-Eingang verarbeiten.

Also für unsere speziellen <select></select> Tags vergleichen wir die Optionen gegen die alten Eingabe. z.B.

@foreach($categories as $category) 
    <option value="{{ $category->id }}" @if(old('category')&&old('category')== $category->id) selected='selected' @endif >{{ $category->category }}</option> 
@endforeach 

Wenn wir ein Element, zum Beispiel ein Produkt bearbeiten, die eine Kategorie hat, wir die aktuelle Kategorie Vorwahlklappung durch die category_id Eigenschaft Vergleich (zum Beispiel) in unserem Produkt. z.B.

@foreach($categories as $category) 
    <option value="{{ $category->id }}" @if($category->id==$product->category_id) selected='selected' @endif >{{ $category->category }}</option> 
@endforeach 

Woher kommt das $ Produkt oder $ Modell oder $ was auch immer? Database, Eloquent, Abfrage, oder einfach nur ein Objekt, das Sie erstellt haben, dann kehren wir es auf die Ansicht mit

return view('product.edit')->withProduct($product); 

oder

return view('product.edit')->with('product',$product); 
+0

da, ich habe meine Frage bearbeitet, werfen Sie einen Blick – David

0

diese Änderung vornehmen und versuchen

   <div class="form-group"> 
       <label>Parent Category</label> 
         <select class="form-control" name="category" id="category" data-url="{{ url('api/dropdown')}}" > 
          <option value=""></option> 
          @foreach($categories as $category) 
           <option value="{{ $category->id }}" {{$product->Category->parent->id == $category->id ? "selected" : "" }}>{{ $category->category}}</option> 
          @endforeach 
         </select> 


        <br> 
       </div> 
+0

Danke Ihr Code ist die Antwort, die ich suchte, ich begegne dem gleichen Problem hehe – Copain