【问题标题】:problem with displaying a stored image from storage/app/public/uploads从 storage/app/public/uploads 显示存储图像的问题
【发布时间】:2021-05-17 22:32:39
【问题描述】:

我正在尝试显示存储文件夹中的一些存储图像,但它没有显示! 这是控制器:

 public function store(Request $request)
    {
            $templates = new templates([
                "image_path"=>$request->image->hashName(),
                "image_path2"=>$request->image2->hashName(),
                "image_path3"=>$request->image3->hashName(),
 ]);

            $request->image->store('public');
            $request->image2->store('public');
            $request->image3->store('public');
            $templates->save();

这里是源图片:

 @foreach($products as $templates)
              <td> <img src= "storage/app/public/uploads/{{$templates->image_path}}"></img></td>
               <td><img src= "storage/app/public/uploads/{{$templates->image_path2}}"></img></td>
               <td><img src="storage/app/public/uploads/{{$templates->image_path3}}"></img></td>

路线如下:

route::get('/view','App\http\controllers\TemplatesController@index');

这里是索引函数:

public function index()
    {
        $products = templates::all();

        return view('products.marja', compact('products'));
    }

【问题讨论】:

  • 您发布的控制器代码处理存储图像,但您说无法显示它们。你能展示一下试图获取它们的操作吗?
  • 不确定,但可能与 $templates 末尾多余的逗号有关,不需要?
  • 据我在您发布的附加信息中看到的,您发送到视图中的变量称为$products,但您尝试使用$templates。您是否收到“未定义变量”通知?
  • @Crimin4L 结尾的逗号是可选的,但不会导致任何问题,它是数组的有效语法。
  • 请分享更多细节。 究竟是什么不起作用?您尝试过什么来解决问题?

标签: php mysql laravel image src


【解决方案1】:

终于成功了!

我创建符号链接:

php artisan storage:link

然后我使用了这个 src 代码:

&lt;img src="storage/{{$templates-&gt;image_path }}" alt=""&gt;&lt;/img&gt;

【讨论】:

    【解决方案2】:

    首先在 .env 文件中创建一个名为 FILESYSTEM_DRIVER 的变量并将其设置为 public 像这样

    FILESYSTEM_DRIVER=public
    

    然后运行创建符号链接。

    php artisan storage:link
    

    它会将您的storage 文件夹复制到public 文件夹中,然后您可以使用asset() 访问您的文件

    <td> <img src= "{{ asset('storage/........./').$templates->image_path }}"></img></td>
    

    它会正常工作的。

    【讨论】:

    • 我照你说的做了,但不幸的是没有任何改变。
    【解决方案3】:

    您可能会在您的/public 中看到一个storage 文件夹,并且您应该注意到它是一个符号链接。 运行以下命令删除符号链接:

    cd public
    rm storage
    cd ..
    

    要创建符号链接,您可以使用storage:link Artisan 命令:

    php artisan storage:link
    

    要在视图中查看图像,请使用以下代码:

    <img src="{{ url('/storage/app/public/uploads/'.$templates->image_path) }}" style="width: 870px; height: 370px;" alt="">
    <img src="{{ url('/storage/app/public/uploads/'.$templates->image_path2) }}" style="width: 870px; height: 370px;" alt="">
    <img src="{{ url('/storage/app/public/uploads/'.$templates->image_path3) }}" style="width: 870px; height: 370px;" alt="">
    

    【讨论】:

    • 这不是符号链接,尽管我按照你说的做了,但没有任何改变。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    • 2020-10-05
    • 2017-09-12
    • 1970-01-01
    • 2020-10-31
    • 2021-07-28
    相关资源
    最近更新 更多