How to Display Saved Images in Laravel 5.5? - php

I am trying to show stored images in img element. I have saved an image by using a Laravel built-in function.
$path = $request->file('image')->store('avatars');
According to the above script, the images are saving to:
Storage > app > avatars
I also tried saving them here:
Storage > app > public > avatars
Afterward, I used the Artisan command: php artisan storage:link
And then in the front end:
<img src="{{ url('storage/'.$Product->image) }}" width="30" height="30"/>
<img src="{{ asset('storage/'.$Product->image) }}" width="30" height="30"/>
<img src="{{ asset($Product->image) }}" width="30" height="30"/>
<img src="{{ asset('public/storage/'.$Product->image) }}" width="30" height="30"/>
However, the image is still not displayed. Can someone kindly let me know where I'm wrong so that I can fix it? I would appreciate that.

Related

Image not showing in .blade file

I'm new to Laravel, but why the hell is this not working?
<img src='../img/netflix_logo.png' />
I've tried using php artisan storage:link and it said The [C:\xampp\htdocs\laravel-crud\public\storage] link has been connected to [C:\xampp\htdocs\laravel-crud\storage\app/public]. The links have been created. but the image is still not displaying in my .blade page..
If you image is in storage/img then
<img src="{{ url('storage/img/netflix_logo.png') }}" alt="" title="" />
If you image is in public/img folder then
<img src="{{ URL::to('/') }}/img/netflix_logo.png" alt="" title="" />
You probably need to use the "asset" helper from laravel/blade:
asset('/path/of/your/image')
It's the common way to call a static file from public folder (including storage if you already linked it)

change logo default notification email laravel 8

I´m traying chenge logo in notification email with laravel 8.
first i published all theme:
php artisan vendor:publish --tag=laravel-notifications
php artisan vendor:publish --tag=laravel-mail
and in header.blade into folder mail/html i´m doing this:
<img src="{{ asset('img/image_default.png') }}" class="logo" alt="Laravel Logo">
my image it´s in public/img
and i´m sending my email ok, but in gmail my logo img it´s broken. I´m using a vhost in localhost
in my email, img route it´s:
googleURLhttp://laravellimpio/img/image_default.png
i checked my asset in other blade and i can show my img ok. I don´t know if i have a problem with my code or it´s for used localhost.
Thanks for help me
Try this
<tr>
<td class="header">
<a href="{{ $url }}" style="display: inline-block;">
#if (trim($slot) === 'Laravel')
<img src="https://laravel.com/img/notification-logo.png" class="logo" alt="Laravel Logo">
#else
<img src="{{ asset('img/image_default.png') }}" class="logo" alt="My logo">
#endif
</a>
</td>
</tr>
As you can see there is a conditional statement checking if the app name is equal to Laravel, change the name of your app from the env file and then remove the {{ $slot }} and use your own path. You need to change the app name so that it skips the first portion of the statement and selects the space between the #else.

Can't retrieve images from storage in Laravel 5.8

I can't retrieve images from storage and I don't know why. I tried many ways but not working. I ran the following command:
php artisan storage:link
I tried these but none of it worked :
<img src="/storage/images/{{$slide->image}}" class="d-block w-100" alt="...">
<img src="{{asset('/storage/images/'.$slide->image)}}" class="d-block w-100" alt="...">
<img src="{{Storage::get('public/images/', $slide->image) }}" class="d-block w-100" alt="...">
php artisan storage:link creates a symbolic link from public/storage to storage/app/public.
So you have to call public/storage/images/ to access the image.
So in your blade you need to link the source to something like this:
<img src="{{ asset('storage/app/images/'.$slide->image) }}"></img>
$url = url('storage/app/public/images/') . $variableName;
<img src='.$url.'>

image working in index.blade.php but not show.blade.php

I am using Laravel to build a Events management system. Users can upload images to be displayed with their event. I have the img tag working on my index.blade.php and assumed I could use the same code on my show.blade.php page, it seems not.
I have the image stored in public/images and I am aware I should use storage for this but I am still learning basics so found this easier for now.
<img style="width:100%" src="images/{{$event->image}}">
As #Frank Ayyaz said you can use a backslash to solve this another Laravel-way is using public_path method which returns the fully qualified path to the public directory.
so you can do something like:
<img style="width:100%" src="{{ public_path('images/'.$event->image) }}">
Please use asset() method. The asset() method is used to include CSS/JavaScript/images files, you can use it as:
{{ URL::asset('images/'.$event->image) }}
<img style="width:100%" src="{{ URL::asset('images/'.$event->image) }}">
You can also use this function for CSS and js.
<link href="{{ asset('css/style.css') }}" rel="stylesheet">
<script src="{{ asset('js/custom.js') }}"></script>
For detail please see this article:
https://medium.com/#zwacky/laravels-url-to-vs-url-asset-fd427ed6f7ef
I suggest Not to use URL::asset but only asset method because sometime URL may cause Problem while showing the image...and use dd() function and check whether u are getting the path i.e Public_path , storage_path() or the custom like the image are stored in public directory..
You may use asset() method of Laravel and use any Prefix whatever you like..
Example:
For static
<img src="{!! asset('assets/images/cams/cam-01.png') !!}" class="img-fluid"
alt="Card image cap">
For Dynamic
<img src="{!! $settings->aws_url.$performer->coverimage !!}" class="img-fluid" alt="Card image cap">
when u use js or css or images u should use {{asset('path')}} to get the resource, it has noting to do with the blade u use, but the resources path.

How can I access in views, images from storage folder in laravel

I have some images stored in the following paths:
/storage/app/public/uploads/users/2/avatar/0d8c18a52732bc9b0068102338fbf29b.jpeg
or
/storage/app/public/uploads/users/3/avatar/0d123dfd32bc9b0068102338fbf29b.jpeg
I had this paths stored in the users table. How can I display it in my view.blade.php file ? I tried smth like this:
<img src="{{ url('/') }}{{ $user->avatar }}" alt="avatar">
But the image is not found on this path. Any idea ?
You need to use the syntax as
<img src='{{ asset("path-to-your-file") }}' alt='avatar'>
instead
<img src="{{ url('/') }}{{ $user->avatar }}" alt="avatar">
asset() points to public folder. path-to-your-file means the rest of the path
Edit
Additionally I have previously posted a similar answer. Please take a look at it too.

Categories