2017-09-04 1 views
0

store has many products은 관계입니다.관계에있는 경로 모델 바인딩

새 제품을 만드는 방법 store_id 및 기타 제품 세부 정보를 저장합니다.

아래 코드를 참조하십시오.

국도 제품 경로와

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

즉 결합 모델 점이다.

모델 Store

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

create product보기.

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

<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 create(){ 
    $storList = Store::all(); 
    return view("createproductview", compact("storList")); 
} 

: 당신이 만드는 제품 페이지에 모든 상점을 통과해야

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

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

둘째 :

답변

0

먼저 당신은 역이 같이 저장 및 제품 사이의 관계를 만들어야합니다 페이지에서 상점 중 하나를 선택하여 상점을 표시하고 유효성 검증 프로세스에서 오류를 관리해야합니다.

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

과 저장 기능 지속 :

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

희망이 도움이 당신에게