Can't show image into image slider in Laravel blade template - php

My image is inside public/storage/slider-images/February2018 location. I retrieve the image name from database but can't show image into my blade template in Laravel though the image is there.
Here is my code of image part in home.blade.php file
#foreach($sliderImage as $data)
{{ HTML::image('public/storage/'.$data->default_img) }}"
#endforeach
And output is http://localhost:8000/public/storage/slider-images/February2018/k8X3qSaqjIYAVYcqapMr.jpg
This url is also not correctly opened in the browser if Itry to open it on the separate tab.
what's the problem in the code ?

Try this code snippet:
#foreach($sliderImage as $data)
$image_location=$data['image_location']; //your image location from db or array
// $image_location = storage/slider-images/February2018/imagename.jpg
<img src="{{ URL::to('/') }}{{ $image_location }}"
#endforeach

I have got my answer.
<img src="storage/{{ $data->default_img }}"
And noted that.
Images should be in public folder.

Related

How do I add images on my laravel project?

I am trying to add images so I can see it on my views. I put the folder img inside the public folder and this is the code I am trying to see.
<?php $image = public_path() . './img/logo.png'; // destination path ?>
<h1>Royal <img src="{{$image}}"> Dice</h1>
All I get is this error in the console: Not allowed to load local resource.
I am learning Laravel but I think that this should be easy to solve. Thank you everyone!
Following should work:
<h1>Royal <img src="{{ asset('img/logo.png' }}"> Dice</h1>
You can find more about the asset helper in the laravel documentation: https://laravel.com/docs/8.x/helpers#method-asset
wite like this <h1>Royal <img src="{{ URL::to($image) }}"> Dice</h1>
or
<h1>Royal <img src="{{ asset($image) }}"> Dice</h1>

Laravel 6 Blade - Display all images inside a public folder?

I am having trouble figuring out a way to reach and display the contents of a folder located in 'public/images'. The goal is to have a blade file that acts as a gallery and displays all the images added to that specific folder.
I know how to access specific files using asset('images/imagename.png') but I need to get all of them at once.
Can I simply create a loop inside of the blade file or do I need to make a controller/route for it?
Try this:
#foreach(File::glob(public_path('images').'/*') as $path)
<img src="{{ str_replace(public_path(), '', $path) }}">
#endforeach
You could do use glob method of the File facade if you know the directory. The following code assumes every file in the public/images/ folder is an image.
<div class="container">
#forelse (File::glob('public/images/*') as $file)
<img src="{{ $file }}">
#else
<p id="no-images">No images in folder</p>
#endforelse
</div>
As the documentation implies, you can get all files in a specific directory with:
Storage::files($directory);
So, you can iterate like this:
#foreach(Storage::files($directory) as $file)
<img src="{{ $file }}">
#endforeach

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.

Laravel image paths from controller to view

My image's path is public/uploads/users/image.png then using src like this:
<img src="{{ asset('uploads/users/image.png') }}" />
But for every user the image has to be changed.
I pass the image name of the user from controller as something like this
foreach ($user as $key) {
$this->data['user_img'] = $key->avatar;
}
Here, I have to change the user image as per $user_img from controller
How should I give it in the src of image tag.
I tried like this way
src="{{ asset('uploads/users/"{{$user_img}}"')}}"
But it seems to be an error for me.How should I resole this.
Somebody help me.
You can use this method for image in laravel.
if this is your image path public/assets/uploads/users/image.png.
image in $user_img
{{asset('assets/uploads/users').'/'.$user_img}}
Or we can use
{{ asset('assets/uploads/users') }}/{{$user_img}}
It is as simple as
<img src="{{ asset('uploads/users/' . $user_img) }}"/>
If that doesn't work the problem is not with the asset helper function but presumably in the value $user_img gets (or doesn't get..)
If you have laravelcollective/html, try this:
{{ Html::image('public/uploads/users/'.$user_img) }}
By the way, did you pass the $user_img to the view from controller ?
Excellent answer as it is a multi-fault protection
src="{{ asset('uploads/users/'.$user_img")}}"
and can use
{{asset('uploads/users').'/'.$user_img}}
other answer.... is scsesfual
try
src="{{ asset(\'uploads/users/$user_img\')}}"
Try Below Code:
<img src="{{URL::asset('assets/images/'.$user_img)}}"/>
Use Laravel Collective HTML Package
After installed this package you can use like this
{!! Html::style('your/css/file.css') !!}
{!! Html::script('your/js/file.js') !!}
{!! Html::image('your/image/file.jpg') !!}
Thank you all,
I can't understand why its not working for me.
But the $user_img retrieve the exact image name.
Then finally I tried like this way.
In controller
foreach ($user as $key) {
$this->data['user_img'] = \URL::to('').'/uploads/users/'.$key->avatar;
}
And in my View I just call the $user_img in the src
src="{{$user_img}}"
Edit
By using
Shrikant Bhardwaj code I gave it as
{{asset('uploads/users').'/'.$user_img}}
Thank you. :)

How can I change the path of images/files inside different views?

This is working fine to show all the images inside the public folder of my home.blade view at www.mygallery.com
#foreach($items as $item)
<div class="box">
<div class="boxInner">
<img src="{{$item->image}}" alt="{{$item->title}}">
</div>
</div>
#endforeach
By the way this is how my routes file looks like.
Route::get('', array('as'=>'itemshome', 'uses'=>'ItemsController#show_items'));
Route::resource('upload', 'ItemsController');
Route::get('tags/{category?}', array('as'=>'itemstag', 'uses'=>'ItemsController#show_items'));
But when I try to filter results by category (www.mygallery.com/tags/cats) $item->image is trying to reach every image at tags/images/myimage.jpg which of course doesn't exist.
The thing is that I don't want to create a public/tags/images folder, so I wonder how can I explicitly point to the correct folder (public/images) no matter what view/route is making the call.
The problem here is that $item->image is a relative path. So depending on your current URL you will link to different paths.
To generate an absolute URL, use the asset() function:
<img src="{{ asset($item->image) }}" alt="{{$item->title}}">

Categories