【问题标题】:upload images and store urls on database with laravel使用 laravel 上传图片并在数据库中存储 url
【发布时间】:2019-07-26 19:27:02
【问题描述】:

我想上传多张图片并将她的 URL 存储到数据库中。

这是我的代码:

<form method="POST" action="{{ route('validated', ['id' => $product->id]) }}) }}">
    @csrf
    <input type="hidden" name="ID" name="ID" value="{{ $product->id  }}">
    <div class="form-group row">
        <label for="Image" class="col-sm-2 col-form-label">Image</label>
        <img src="{{ $product->imageURL }}" />
    </div>
    <div class="form-group row">
        <div class="col-sm-12">
            <button type="submit" class="btn btn-lg btn-success">Valider les données</button> | <a href="#">Relancer le fournisseur</a>
        </div>
    </div>
</form>

进入我的控制器功能:

$imagePath = Storage::putFile('uploads/image/', $request->image, "imagename");

我有这个错误:

Symfony\Component\Debug\Exception\FatalThrowableError 抛出 消息“调用字符串上的成员函数 hashName()”

Stacktrace: 0 Symfony\Component\Debug\Exception\FatalThrowableError in C:\laragon\www\MyProject\vendor\laravel\framework\src\Illuminate\Filesystem\FilesystemAdapter.php:208

【问题讨论】:

  • 您的表单缺少&lt;input type="file" name="image" /&gt; 元素。并在表单标签&lt;form method="POST" action="{{ route('validated', ['id' =&gt; $product-&gt;id]) }}) }}" enctype="multipart/form-data"&gt;中添加属性enctype="multipart/form-data"
  • 也使用$request-&gt;file('image') 而不是$request-&gt;image

标签: laravel image upload


【解决方案1】:

如果你想上传图片,首先你的表单必须有一个enctype="multipart/form-data" 属性。

基于Laravel documentation,您可以使用如下内容:

$path = $request->file('image')->store('image');

【讨论】:

  • 在这一行 $path 上调用数组上的成员函数 store()
【解决方案2】:

您好,我认为您需要使用 putFileAs 方法来自定义名称,例如:

//Manually specify a file name...
Storage::putFileAs('photos', new File('/path/to/photo'), 'photo.jpg');

此方法接受 Illuminate\Http\File 或 Illuminate\Http\UploadedFile 实例,并将自动将文件流式传输到您想要的位置:

source

【讨论】:

  • 嗨,谢谢你的回答,我也试过了,但我还是同样的错误
  • 是的 @george-hanson 是对的,你没有文件上传输入,你需要它的名称为 image 来发出请求和 enctype="multipart/form-data " 也是一部分。
【解决方案3】:

对于多张图片上传,你可以试试这个代码

$images = $request->file('images');
foreach ($images as $key => $image) {

    if ($request->hasFile('images') && $request->file('images')[$key]->isValid()) {
        $urlPath = $request->images[$key]->store('public/images');

        $image = new Images();
        $image->path = $urlPath;
        $image->save();
    } else {
        return redirect()->back()->withInput()->with('errors', 'Invalid Image file found!');
    }
}

假设您必须在表单中添加 enctype="multipart/form-data",在函数参数中添加 Request $request,在公共目录中添加 storage link

我希望它会有所帮助。谢谢

【讨论】:

    【解决方案4】:

    如果你使用 Laravel,你应该能够做到以下几点:

    $imagePath = $request->file('image')->store('uploads/image');
    

    或者如果你想指定文件名,你可以这样做:

    $imagePath = $request->file('image')->storeAs('uploads/image', 'myfilename.jpg');
    

    更新

    我注意到您的 HTML 表单中没有文件上传输入。如果你没有,Laravel 就没有文件要存储。

    【讨论】:

    • 嗨,谢谢你的回答,我试过了,但我得到了这个错误:Call to a member function store() on null
    • 将您的表单标签更改为&lt;form method="POST" action="{{ route('validated', ['id' =&gt; $product-&gt;id]) }}) }}" enctype="multipart/form-data"&gt;
    【解决方案5】:

    如果您想编写可重用的代码并以不同的方式使用它,请执行此操作

    例如我们有 Post 和 Image 模型 在图像模型中添加此代码

    public function post()
        {
            return $this->belongsTo(Post::class);
        }
     public static function named($name,$type)
    {
        $photo = new static;
        $photo->saveAs($name,$type);
        return $photo;
    }
    public function saveAs($name,$type)
    {
        $this->path = sprintf("%s/%s/%s-%s",'baseDir',$type,time(), $name);
        $this->name = sprintf("%s-%s", time(), $name);
        $this->type = $type;
    }
    public function moves(UploadedFile $file,$type)
    {
        $file->move($this->baseDir.'/'.$type, $this->name);
        return $this;
    }
    

    并在您想要为其添加图像的模型中,例如 Post :

        public function images()
        {
            return $this->hasMany(Image::class);
        }
         public function addImages(Image $image)
        {
            return $this->images()->save($image);
        }
    

    在控制器中:

    public function addImages(ImageRequest $request,$id)
     {
        $image = $this->makeImage($request->file('file'),$request->type);
        Post::where('id' ,'=', $id)->first()->addImage($image);
        return back();
     }
    
     protected function makeImage(UploadedFile $file,$type)
       {
         return Image::named($file->getClientOriginalName(),$type)->moves($file,$type);
    
       }
    

    【讨论】:

      猜你喜欢
      • 2020-11-30
      • 2017-11-05
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      • 2016-10-24
      相关资源
      最近更新 更多