简体   繁体   English

laravel:如何将文件上传的路径存储在数据库中

[英]laravel : How to Store file uploaded path in database

Here is my controller code : 这是我的控制器代码:

 public function save(Request $request) {
    try {

        $this->validate($request, Country::rules());


        If(Input::hasFile('flag_image')){
            $file = Input::file('flag_image');
            $destinationPath = public_path(). '/images/admin/country/';
            $filename = $file->getClientOriginalName();
            $image = time().$filename;
            $file->move($destinationPath, $image);
            $imgpath = 'public/images/admin/country/'.$image;
        }
        $request['flag_image'] = $imgpath;
        $country = Country::saveOrUpdate($request);
        if($country !== false) {
            return redirect()->route('lists-country')->with('success', trans('Country data added successfully.!!'));
        } else {
            return back()->with('error', "Unable to save country data.!!")->withInput();
        }
    } catch (\Exception $ex) {
        return back()->with('error', "Unable to save country data.!!")->withInput();
    }
}

When I dd($request) before saveOrUpdate function I got the full path of the image uploaded file. 当我在saveOrUpdate函数之前dd($request) ,我得到了图像上传文件的完整路径。

 +request: ParameterBag {#40 ▼
#parameters: array:6 [▼
  "_token" => "c94UG3R2PbbdHsXRLzs9OjEBTna23OHINFpki63U"
  "id" => ""
  "title" => "INDIA"
  "short_name" => "IN"
  "status" => "Active"
  "flag_image" => "public/images/admin/country/1515577652banner5.jpg"
]}

After successfully adding data on table a flag_image path look like "D:\\xampp\\tmp\\php63D3.tmp" i want to store "public/images/admin/country/1515577652banner5.jpg" this path. 在表上成功添加数据后,flag_image路径看起来像"D:\\xampp\\tmp\\php63D3.tmp"我想存储"public/images/admin/country/1515577652banner5.jpg"这条路径。

Just do 做就是了

$country->flag_image = $imgpath;
$country->save();

after

$country = Country::saveOrUpdate($request);

You need to merge() it: 你需要merge()它:

$request->merge(['flag_image' => $imgpath]);

Instead of: 代替:

$request['flag_image'] = $imgpath;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM