【问题标题】:Laravel storage imagesLaravel 存储镜像
【发布时间】:2021-05-29 19:52:59
【问题描述】:

我的图像工作正常,但现在我的存储目录有问题。

在他们去之前:

但现在他们要去这里:

我的代码配置文件控制器:

        if($r->hasFile('profile_avatar')){
            Auth::user()->update([
            'profile_avatar' => $r->profile_avatar->store('public/avatars')
            ]);
        }

        if($r->hasFile('banner')){
            Auth::user()->profile()->update([
            'banner' => $r->banner->store('public/avatars')
            ]);
        }

        Session::flash('success', 'Perfil Atualizado com Sucesso!');
        return redirect()->back();
    }

以前可以,但是现在不显示任何图片

 <img src="{{ Storage::url($user->profile_avatar) }}" width="400px" height="400px" style="border-radius: 50%; display: inline-block" alt="" class="unselectable">

【问题讨论】:

  • $r->profile_avatar->store('public/avatars') $r 有这个存储功能吗?似乎应该控制保存目录结帐:laravel.com/docs/8.x/filesystem
  • 检查 config/filesystem.php 文件以配置您的默认存储驱动程序及其路径

标签: php html laravel web


【解决方案1】:

嗯,那是因为你做错了。

如果您真的打算定义要在其中存储图像文件的 磁盘,则必须将 disk 名称作为 @987654322 的第二个参数传递@方法。

Specifying A Disk

// Instead of: ❌
'profile_avatar' => $r->profile_avatar->store('public/avatars')

// Use this: ✅
'profile_avatar' => $r->file('profile_avatar')->store( 'avatars', 'public')

.

// Instead of: ❌
'banner' => $r->banner->store('public/avatars')

// Use this: ✅
'banner' => $r->file('banner')->store( 'avatars', 'public')

【讨论】:

    【解决方案2】:

    在 laravel 中使用资产来解决问题,例如:

    {{asset('avatars/imagename.png')}}
    

    代替:存储方式,同时显示图片。

    【讨论】:

    • 我相信这不是这里的问题。他的方法也很有效,因为他已经在public/storage 定义了一个符号链接,它指向storage/app/public 目录。阅读更多:File URLs
    猜你喜欢
    • 1970-01-01
    • 2020-11-28
    • 2015-07-16
    • 2013-03-30
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 2012-08-14
    • 2013-08-01
    相关资源
    最近更新 更多