2017-09-04 4 views
0

A store has many products ist die Beziehung.Route Modell Bindung in Beziehungen

So erstellen Sie ein neues Produkt, speichern store_id und andere Produktdetails.

Der Code wie folgt.

Strecke ist

Route::resource('stores.product', 'productcontroller'); 

d.h. Bindungsmodellspeicher mit Produkt Route.

Modell Store

class store extends Model 
{ 
    public function product() 
    { 
     return $this->hasMany(product::class); 
    } 
} 

create product Ansicht.

<form method="POST" action="/stores/{{$store->id}}/product" enctype="multipart/form-data"> 
    {{ csrf_field() }} 
    <div class="form-group"> 
     name <input type="text" name="name" /> 
    </div> 

[email protected]

public function store (store $store, Request $request) 
{ 
    $this->validate($request, [ 
     'name' => 'required|max:255', 
     'detail' => 'nullable' , 
    ]); 

    $product = new product; 
    $product-> user_id = auth()->id(); 
    $product-> store_id = $store->id; 
    $product-> name = $request->name; 
    $product->save(); 
    return redirect('/stores/{{$store->id}}/product'); 
} 

Bitte erläutern Sie, wie Route Modell funktioniert in Beziehungen zu binden.

Was sollte die Methode und die Aktion meiner Erstellungsform sein?

Wo sollte [email protected] Return Redirect?

Antwort

0

Zuerst haben Sie die inverse Beziehung zwischen Geschäften und Produkten wie diese zu erstellen:

class Store extends Model 
{ 
    public function products() 
    { 
     return $this->hasMany(Product::class); 
    } 
} 

class Product extends Model 
{ 
    public function store() 
    { 
     return $this->belonsTo(Store::class); 
    } 
} 

Zweite Sie alle Ihre Geschäfte zu Ihrer erstellen Produktseite passieren müssen:

public function create(){ 
    $storList = Store::all(); 
    return view("createproductview", compact("storList")); 
} 

Darin Seite müssen Sie die Filialen zeigen, um eine von ihnen zu wählen, und verwalten Sie Ihre Fehler aus dem Validierungsprozess:

<form method="POST" action="{ route("product.store") }}" enctype="multipart/form-data"> 
    {{ csrf_field() }} 
    <div class="form-group {{ ($errors->has('store'))?"has-error":"" }}"> 
     <label for="store">Store</label> 
     <select class="form-control" name="tipoaudiencia" id="store"> 
      <option value="">Select one option</option> 
      @foreach($storList as $row) 
       <option {{ (old("store") == $row->id ? "selected":"") }} value="{{ $row->id }}">{{ $row->name }}</option> 
      @endforeach 
     </select> 
     @if($errors->has('store'))<p class="help-block">{{ $errors->first('store') }}</p>@endif 
    </div> 
    <div class="form-group required {{ ($errors->has('name'))?"has-error":"" }}"> 
     <label for="name">Name</label> 
     <input type="text" class="form-control" id="name" placeholder="name" name="name" value="{{ old('name') }}" autofocus> 
     @if($errors->has('name'))<p class="help-block">{{ $errors->first('name') }}</p>@endif 
    </div> 
    ... 
</form> 

und zuletzt die Speicherfunktion:

public function store (Request $request) 
{ 
    $this->validate($request, [ 
     'name' => 'required|max:255', 
     'store' => 'required' 
    ]); 

    $product = new Product; 
    $product->name = $request->name; 
    $store = Store::find($request->store); 
    $store->products()->save($product);//this saves the product and manage the relation. 
    return redirect('/stores/'.$store->id.'/'.$product->id); 
} 

Hope this helfen Ihnen