【问题标题】:Route Model Binding in Relationships关系中的路由模型绑定
【发布时间】:2018-02-12 22:53:12
【问题描述】:

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查看。

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

productController@store

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

请解释路由模型绑定在关系中的工作原理。

我的创建表单的方法和操作应该是什么?

productController@store 应该在哪里返回重定向?

【问题讨论】:

    标签: forms laravel eloquent


    【解决方案1】:

    首先,您必须创建商店和产品之间的反比关系 像这样:

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

    其次,您必须将所有商店传递到您的创建产品页面:

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

    在该页面中,您必须显示商店以选择其中之一,并管理验证过程中的错误:

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

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2018-01-18
      • 2018-10-07
      • 2018-12-17
      • 2019-09-05
      • 2019-01-10
      • 2016-06-04
      • 1970-01-01
      • 2019-09-14
      • 1970-01-01
      相关资源
      最近更新 更多