这是添加的时候 可以上传照片

/*** * 添加商户 * @return IlluminateContractsViewFactory|IlluminateViewView */ public function add() { $data = null; return _view('admin.merchant.merchant.edit', compact; } /*** * 添加商户 * @return IlluminateContractsViewFactory|IlluminateViewView */ public function store(StoreMenchantRequest $request) { //判断手机号是否重复 重复不能添加 //后面开发可能会去掉这个判断 $merchant = Merchant::where('mobile', $request->mobile)->first(); if  { return back()->withErrors; } $token = str_random; $api_token = $this->getToken; $newMerchantData = [ 'mobile' => $request->mobile, 'api_token' => $api_token, ]; DB::beginTransaction(); $newMerchant = Merchant::create; $newData = [ 'merchant_id' => $newMerchant->id,//Merchantid 'merchant_principal' => $request->merchant_principal,//负责人 'merchant_name' => $request->merchant_name,//商家名称 'merchant_short_name' => $request->merchant_short_name,//商家简称 'merchant_address' => $request->merchant_address,//商家地址 'business_num' => $request->business_num,//注册号 'business_address' => $request->business_address,//营业地址 'business_name' => $request->business_name,//营业执照名称 'business_person' => $request->person,//营业执照法人 'identity_name' => $request->person,//身份证姓名 'identity_num' => $request->identity_num,//身份证号 ]; //上传缩略图 $input = $request->all(); if  && is_object { $file_name = save_image_file($input['file'], 'merchant_infos'); if  { return back()->with; }// dd; $input['thumbnail'] = $file_name; unset; unset; } else { return back()->with; } //上传内景图1 if (isset && is_object { $file_name_1 = save_image_file($input['image1'], 'merchant_infos'); if  { return back()->with; } $input['interior_figure_one'] = $file_name_1; unset; unset; } else { return back()->with; } //上传内景图2 if (isset && is_object { $file_name_2 = save_image_file($input['image2'], 'merchant_infos'); if  { return back()->with; } $input['interior_figure_two'] = $file_name_2; unset; unset; } else { return back()->with; } //上传内景图3 if (isset && is_object { $file_name_3 = save_image_file($input['image3'], 'merchant_infos'); if  { return back()->with; } $input['interior_figure_three'] = $file_name_3; unset; unset; } else { return back()->with; } $merchantInfo = MerchantInfo::where('merchant_id', $newMerchant->id)->first(); if ) { return back()->withErrors; } $homestayInfo = HomestayInfo::where('merchant_id', $newMerchant->id)->first(); if ) { return back()->withErrors; } //录入商户信息 $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; $newHomestayInfo = HomestayInfo::create; if ($newMerchantInfo && $newHomestayInfo && $newMerchant) { DB::commit(); admin_action_logs($newMerchant, "添加商户成功"); return redirect()->route('admin.merchant.index')->with; } else { DB::rollback->withErrors; } }

这边封装了一个上传图片的方法,调用即可

   {{ Form::open(['method'=>'post','route' => ['admin.merchant.add_img_store'],'enctype'=>'multipart/form-data']) }} 商户图片       * 缩略图: :   @if  @else  @endif           * 内景图1: :   @if  @else  @endif           * 内景图2: :   @if  @else  @endif           * 缩略图3: :   @if  @else  @endif          编辑商户  {{ Form::close() }}  

接下来是编辑时候 显示已经上传的图片 并且可以进行修改:

** * 调用的文件中需要 use IlluminateSupportFacadesInput; IlluminateSupportFacadesStorage; * save_image_file 保存图片文件 ,存在Storage::disk 目录下 * @var $file object 上传的图片文件,具体是在 request 中的 UploadedFile 类型的对象 * @var $prefix_name string 可选保存的文件名前缀 * @var $path string 文件路径 * @return bool/string 如果通过验证 则返回在新的文件名 */if (!function_exists { function save_image_file(&$file, $prefix_name = '', $path = 'serve') { $file = isset ? $file : null; if ($file != null && $file->isValid { // 获取文件相关信息 $originalName = $file->getClientOriginalName(); // 文件原名 $ext = $file->getClientOriginalExtension; $file->getClientOriginalName(); if ($ext == "" && $file->getClientOriginalName { $ext = 'png'; } if (!preg_match('/jpg|png|gif$/is', $ext)) { return false; } //dd; $realPath = $file->getRealPath(); //临时文件的绝对路径 $type = $file->getClientMimeType(); // image/jpeg // 上传文件 $filename = $prefix_name . '-' . date . '-' . uniqid() . '.' . $ext; //dd; $bool = Storage::disk->put($filename, file_get_contents; if  return false; return $filename; } return false; }}
/*** * 添加图片 * @return IlluminateContractsViewFactory|IlluminateViewView */public function add_img(){ $data = null; return _view('admin.merchant.merchant.add', compact;} /*** * 保存图片 * @return IlluminateContractsViewFactory|IlluminateViewView */public function add_img_store{ //上传缩略图 $input = $request->all(); if  && is_object { $file_name = save_image_file($input['file'], 'merchant_infos'); if  { return back()->with; } $input['thumbnail'] = $file_name; unset; unset; } else { return back()->with; } //上传内景图1 if (isset && is_object { $file_name_1 = save_image_file($input['image1'], 'merchant_infos'); if  { return back()->with; } $input['interior_figure_one'] = $file_name_1; unset; unset; } else { return back()->with; } //上传内景图2 if (isset && is_object { $file_name_2 = save_image_file($input['image2'], 'merchant_infos'); if  { return back()->with; } $input['interior_figure_two'] = $file_name_2; unset; unset; } else { return back()->with; } //上传内景图3 if (isset && is_object { $file_name_3 = save_image_file($input['image3'], 'merchant_infos'); if  { return back()->with; } $input['interior_figure_three'] = $file_name_3; unset; unset; } else { return back()->with; } //录入商户信息 $merchang_info = MerchantInfo::where('merchant_id', '=', $input['id'])->first(); if ) { $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['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;*/ 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 { DB::commit(); admin_action_logs; return redirect()->route('admin.merchant.index')->with; } else { DB::rollback->withErrors; }}

先看控制器:

这是编辑的时候 可以修改照片

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

首先先看下效果图

以上这篇laravel实现图片上传预览,及编辑时可更换图片,并实时变化的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

代码部分:

发表评论

电子邮件地址不会被公开。 必填项已用*标注