Laravel 5 - 更新多态关系

时间:2015-07-31 00:46:24

标签: php laravel polymorphism relationship

我在图像和产品模型之间存在多态关系。

图片模型:

class Image extends Model {

   protected $table = 'images';

   public function imageable()
   {
       return $this->morphTo();
   }
}

产品型号:

class Product extends Model {

   protected $table = 'products';

   public function images()
   {
       return $this->morphMany('App\Image', 'imageable');
   }
}

每个产品都有3张图片(照片),因此当我编辑产品时,我只编辑1张图像,然后......

如何检测编辑的图像以进行更新?

产品控制器更新方法:

public function update($id, CreateProductRequest $request)
{
    \DB::transaction(function() use ($id, $request)
    {
        $product               = Product::find($id);
        $product->name         = $request->name;
        $product->description  = $request->description;
        $product->price        = $request->price;

        //Images upload
        //HERE I NEED TO KNOW WHICH IMAGE WAS EDITED TO UPDATE IT ON IMAGES TABLE

        $product->save();
    });

    session()->flash('message', 'Product updated successfully');

    return redirect()->action('ProductController@index');
}

查看结构:

<div class="form-group">
   <label class="col-md-3 col-xs-12 control-label">Image 1</label>
   <div class="col-md-6 col-xs-12">
      <input id="photo1" name="images[]" class="file" type="file">
   </div>
</div>

<div class="form-group">
   <label class="col-md-3 col-xs-12 control-label">Image 2</label>
   <div class="col-md-6 col-xs-12">
      <input id="photo2" name="images[]" class="file" type="file">
   </div>
</div>

<div class="form-group">
   <label class="col-md-3 col-xs-12 control-label">Image 3</label>
   <div class="col-md-6 col-xs-12">
      <input id="photo3" name="images[]" class="file" type="file">
   </div>
</div>

0 个答案:

没有答案