如何使用Laravel 5.8显示图像? [英] How to display image using Laravel 5.8?

查看:47
本文介绍了如何使用Laravel 5.8显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示本地存储的图像.那些上传的文件位于存储"文件夹中

我正在使用以下代码来显示图像

  @foreach($ publications为$ p)< div class ="card" style ="width:18rem;">< img class ="card-img-top" src ="{{URL('public/storage/app/{$ p-> file_name}')}}""alt =" {}}"/>< div class ="card-body">< p class ="card-text"> {{$ p-> description}}</p></div></div>@endforeach 

但是我得到了

怎么了?

编辑1

我使用 php artisan storage:link ,显示了控制台

  [public/storage]目录已链接. 

但是它仍然不起作用

解决方案

首先,您需要做

  php artisan存储:链接 

,以便wepapp无法访问的存储文件夹通常与您的公众链接文件夹,以便您的应用可以访问图片,就像它们在公共文件夹中一样但会将它们存储在存储文件夹中,那么您应该像这样放置smthg来显示图像

我认为会是这样

 < img src ="/storage/2/{{$ p-> file_name}}"/> 

如果此路径不起作用,您可以在应用程序中编辑该路径

希望它可以与您一起使用

编辑,我将与您分享我代码中的示例在迁移中,用户迁移

  $ table-> string('image'); 

控制器

  $ imageName = time().'.'.request()-> image-> getClientOriginalExtension();request()->图像->移动(public_path('userpicture'),$ imageName);返回User :: create(['名称'=>$ data ['name'],'email'=>$ data ['email'],'图像'=>$ imageName,'password'=>哈希:: make($ data ['password']),]); 

请注意,我在此处使用的create方法仅在将那些可填充项填充到用户模型中后才有效,所以没关系,您可以在存储数据部分中使用所需的任何内容,但是要在这三行中简单介绍

  $ imageName = time().'.'.request()-> image-> getClientOriginalExtension();request()->图像->移动(public_path('userpicture'),$ imageName);'图像'=>$ imageName, 

如您所见,移动功能是将图片移动到公用文件夹中并将其放入userpicture文件夹中,然后最后一行将图片名称存储在数据库中,以便我以后可以访问它以显示图片

然后我可以简单地执行此操作以显示图片

 < img class ="img响应式img舍入" src ="{{asset('userpicture/1561335133.jpg')}}""alt ="用户图片> 

I am trying to display image stored in my local. Those file uploaded are in Storage folder

I am using the follow code to display the image

@foreach($publications as $p)
    <div class="card" style="width: 18rem;">
        <img class="card-img-top" src="{{URL('public/storage/app/{$p->file_name}')}}" alt="{{$p->file_name}}" />
      <div class="card-body">
        <p class="card-text">{{$p->description}}</p>
      </div>
    </div>
@endforeach

But I got:

what's wrong?

EDIT 1

I use php artisan storage:link, the console displayed

The [public/storage] directory has been linked.

But it still not working

解决方案

First you need to do

php artisan storage:link

so that your storage folder which the wepapp cannot access usually links with your public folder so that your app can access the pictures like if they are in the public folder but it will store them in the storage folder then you should put smthg like this to display the image

i think it will be like this

    <img src="/storage/2/{{$p->file_name}}"/>

you could edit the path to the path in your application if this path didnt work

hope it works with you

Edit i will share with you an example from my code in migration the users migration

$table->string('image');

controller

$imageName = time().'.'.request()->image->getClientOriginalExtension();
        request()->image->move(public_path('userpicture'), $imageName);
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'image' => $imageName,
            'password' => Hash::make($data['password']),
        ]);

note that the create method i am using here works only when you put those fildes fillable in the user model so never mind you could use whatever you want in the storing data part but concetrate on this three lines

$imageName = time().'.'.request()->image->getClientOriginalExtension();
            request()->image->move(public_path('userpicture'), $imageName);

                'image' => $imageName,

as you can see the move function is moving the picture to the public folder and putting it in userpicture folder and then the last line store the image name in the database so that i could access it later on to display the picture

and then i could just simply do this to display the picture

<img class="img-responsive img-rounded" src="{{ asset('userpicture/1561335133.jpg') }}" alt="User picture">

这篇关于如何使用Laravel 5.8显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆