laravel实现图片上传预览,及编辑时可更换图片,并(3)

编辑这边 的控制器代码是:

/***
 * 添加图片
 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
 */
public function add_img()
{
  $data = null;
  return _view('admin.merchant.merchant.add', compact('data'));
}
 
/***
 * 保存图片
 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
 */
public function add_img_store(Request $request)
{
  //上传缩略图
  $input = $request->all();
 
  if (isset($input['file']) && is_object($input['file'])) {
 
    $file_name = save_image_file($input['file'], 'merchant_infos');
    if (!$file_name) {
      return back()->with('msg', '图片上传失败,请重试!');
    }
 
    $input['thumbnail'] = $file_name;
    unset($input['_token']);
    unset($input['file']);
  } else {
    return back()->with('msg', '请上传图片');
  }
  //上传内景图1
  if (isset($input['image1']) && is_object($input['image1'])) {
 
    $file_name_1 = save_image_file($input['image1'], 'merchant_infos');
    if (!$file_name_1) {
      return back()->with('msg', '图片上传失败,请重试!');
    }
 
    $input['interior_figure_one'] = $file_name_1;
    unset($input['_token']);
    unset($input['image1']);
  } else {
    return back()->with('msg', '请上传图片');
  }
  //上传内景图2
  if (isset($input['image2']) && is_object($input['image2'])) {
 
    $file_name_2 = save_image_file($input['image2'], 'merchant_infos');
    if (!$file_name_2) {
      return back()->with('msg', '图片上传失败,请重试!');
    }
    $input['interior_figure_two'] = $file_name_2;
    unset($input['_token']);
    unset($input['image2']);
  } else {
    return back()->with('msg', '请上传图片');
  }
  //上传内景图3
  if (isset($input['image3']) && is_object($input['image3'])) {
 
    $file_name_3 = save_image_file($input['image3'], 'merchant_infos');
    if (!$file_name_3) {
      return back()->with('msg', '图片上传失败,请重试!');
    }
    $input['interior_figure_three'] = $file_name_3;
    unset($input['_token']);
    unset($input['image3']);
  } else {
    return back()->with('msg', '请上传图片');
  }
  //录入商户信息
 
  $merchang_info = MerchantInfo::where('merchant_id', '=', $input['id'])->first();
  if (empty($merchang_info)) {
    $newData['thumbnail'] = $input['thumbnail'];
    $newData['merchant_id'] = $input['id'];
    $newData['interior_figure_one'] = $input['interior_figure_one'];
    $newData['interior_figure_two'] = $input['interior_figure_two'];
    $newData['interior_figure_three'] = $input['interior_figure_three'];
    $newData['content']='';
    $result = MerchantInfo::create($newData);
  } /* $newData['thumbnail']=$input['thumbnail'];
  $newData['interior_figure_one']=$input['interior_figure_one'];
  $newData['interior_figure_two']=$input['interior_figure_two'];
  $newData['interior_figure_three']=$input['interior_figure_three'];
  // $newData['content']=$input['content'];
  $newMerchantInfo = MerchantInfo::create($newData);*/
  else {
    $merchang_info->thumbnail = $input['thumbnail']??'';
    $merchang_info->interior_figure_one = $input['interior_figure_one']??'';
    $merchang_info->interior_figure_two = $input['interior_figure_two']??'';
    $merchang_info->interior_figure_three = $input['interior_figure_three']??'';
    $result = $merchang_info->save();
  }
  if ($result) {
    DB::commit();
    admin_action_logs($result, "编辑商户成功");
    return redirect()->route('admin.merchant.index')->with('msg', '编辑成功');
  } else {
    DB::rollback();
    return back()->withErrors('编辑失败,请联系管理员');
  }
}

      

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/1901.html