如何在Laravel 5.4中更新多个图像并删除旧图像?

时间:2018-05-28 21:58:28

标签: laravel-5.4

我很难更新和替换多个图像。我理解如何保存多个图像,但我无法更新和删除旧图像。

  • 这是3个表格:
    • 组合
    • 照片
    • portfolio_photo(pivot)

HTML表单更新

{!! Form::model($portfolio, ['route' => ['portfolio.update', $portfolio->id], 'method' => 'PUT', 'files' => 'true' ]) !!}
    <input type="file" id="portfolio_photos" name="portfolio_photos[]" class="form-control" multiple>
{{!! Form::close() !!}}

控制器,我不知道如何更新方法。

    public function store(PortfolioValidation $request){

      $portfolio = new Portfolio();
      $portfolio->portfolio_title = $request->portfolio_title;
      $portfolio->portfolio_slug = str_slug($request->portfolio_title);
      $portfolio->apartment_project = $request->apartment_project;
      $portfolio->apartment_area = $request->apartment_area;
      $portfolio->type_repair = $request->type_repair;
      $portfolio->year_implementation = $request->year_implementation;
      $portfolio->deadline = $request->deadline;
      $portfolio->save();

      if($request->hasFile('portfolio_photos')){
         foreach($request->portfolio_photos as $portfolio_photo){
            $fileName = rand() . '.' . $portfolio_photo->getClientOriginalExtension();
            $path = public_path('img/portfolio/' . $fileName);
            Image::make($portfolio_photo)->resize(1280, 960)->save($path);
            $newFile = new Photo();
            $newFile->portfolio_photos = $fileName;
            $newFile->save();
            $portfolio->photos()->save($newFile);
         }
      }
      Session::flash('success', 'Success!');
      return redirect()->route('portfolio.index');
   }

0 个答案:

没有答案