【问题标题】:How to display Image saved in storage folder on blade如何在刀片上显示保存在存储文件夹中的图像
【发布时间】:2021-09-21 04:20:51
【问题描述】:

我想在刀片视图中显示保存在存储目录中的图像。我成功地将文件存储到 storage/app/public 文件夹中,就像这样 ProfilesController 文件:

`if(request('image')) {
            $imagePath = request('image')->store('profile', 'public');

            $image = Image::make(public_path("storage/{$imagePath}"))->fit(1000, 1000);
            $image->save();
        }

        auth()->user()->profile->update(array_merge(
            $data,
            ['image' => $imagePath] 
        ));`

现在我想在我的 index.blade.php 文件中检索图像。这是怎么做到的:

<img src="/storage/{{$user->profile->image}}" alt="">

laravel 文档说在我的终端中使用存储链接我这样做了

php artisan storage:link

我尝试了所有方法,但视图中没有图像加载!

我做错了什么以及如何解决!

【问题讨论】:

  • {$user->profile->image}} 包含什么内容?

标签: php html laravel storage laravel-blade


【解决方案1】:

这就是我要解决的方法。实际的解决方案是根据文档通过 storage:link 命令将存储目录与公用文件夹链接 enter link description here

if(request('image')) {
       $imageName=time().'.' . $request->file('image')->extension();

        $imagePath = request('image')->storeAs('profile', $imageName);

        //you can use $imagePath to save the name as well as the path of the image
        $image->save();
    }

    ;`

在您的 config 文件夹中的 filesystems.php 文件中,附加配置文件文件夹以匹配公共文件夹,如下所示:

'links' => [
    public_path('storage') => storage_path('app/public'),
    public_path('profile') => storage_path('app/profile'),

],

确保重新运行命令:

php artisan storage:link

为了访问图像,

<img src="{{asset($user->profile->image)}}" alt="">

【讨论】:

    【解决方案2】:

    您应该在用户配置文件模型中使用访问器,即:-

    public function getImageAttribute($image) { return asset('storage'.$image); }

    【讨论】:

      【解决方案3】:
          <img class="" src="{{ Storage::disk('name-option')->url('full path.jpg') }}"  alt="">
      

      disk('name-option') 是可以跳过的选项

      <img class="" src="{{ Storage::url('full path.jpg') }}"  alt="">
      

      【讨论】:

        猜你喜欢
        • 2019-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-25
        • 2019-11-13
        • 2018-10-27
        • 1970-01-01
        相关资源
        最近更新 更多