【问题标题】:laravel uploading image in database with wrong pathlaravel在数据库中上传图像路径错误
【发布时间】:2020-05-11 16:58:21
【问题描述】:

将图像上传到数据库会给出错误的路径。

它应该引用我的存储/上传目录

我也做了这个命令来公开存储文件夹。

php artisan storage:link

编码

enctype="multipart/form-data"

我的代码:

public function store(Posts $posts){
    $validation = request()->validate([
        'name' => 'required',
        'ingredients' => 'required',
        'prepartion' => 'required',
        'categorie_id' => 'required',
        'image' => 'required|file|image|max:5000'
    ]);

    Posts::create($validation);

    $posts->update([
        'image' => request()->image->store('uploads', 'public')
    ]);

    return redirect('/admin');
}

如果需要更多信息,请告诉我

【问题讨论】:

标签: database laravel upload


【解决方案1】:

您可以调用 Storage 门面的 putFile 方法来执行相同的文件操作

$posts->update([
     'image' => \Storage::putFile('uploads', request()->file('image'));
]);

【讨论】:

    【解决方案2】:

    您应该拥有存储外观并使用应该在 config/filesystem.php 中定义的磁盘来存储图像。

    Storage::disk('public')->put($path, $content);
    

    【讨论】:

      猜你喜欢
      • 2015-08-23
      • 1970-01-01
      • 2013-07-18
      • 2018-10-27
      • 1970-01-01
      • 2020-06-06
      • 2018-05-09
      • 2015-11-06
      • 1970-01-01
      相关资源
      最近更新 更多