1

我想在刀片视图中显示保存在存储目录中的图像。我成功地将文件存储到 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

我尝试了所有这些,但是视图中没有图像加载!

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

4

3 回答 3

0

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

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

于 2021-07-12T06:42:37.897 回答
0
    <img class="" src="{{ Storage::disk('name-option')->url('full path.jpg') }}"  alt="">

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

<img class="" src="{{ Storage::url('full path.jpg') }}"  alt="">
于 2021-07-12T01:11:17.960 回答
0

这就是我将如何解决它。实际的解决方案是根据文档通过 storage:link 命令将存储目录与公用文件夹链接, 在此处输入链接描述

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="">
于 2022-02-16T15:58:38.967 回答