TuGordoBello
TuGordoBello

Reputation: 4510

How to display image using Laravel 5.8?

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

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:

enter image description here

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

Upvotes: 1

Views: 6454

Answers (2)

RYOK
RYOK

Reputation: 473

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">

Upvotes: 4

Robbin Benard
Robbin Benard

Reputation: 1652

php artisan storage:link creates a symlink to /storage/app/public

But it looks like your files are in /storage/app/files try moving the files to the public folder.

you can use the asset helper to show the file {{ asset("storage/{$p->file_name}") }}

Upvotes: 2

Related Questions