Laravel image paths from controller to view - php

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. :)

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>

How to make an asset to show dynamically in Laravel

I'm trying to fix an issue with the avatar image in the header. If I use the following URL it works fine :
src="{{asset('user/uploads/avatars/1562309192.jpg') }}"
While if I try to make it dynamic and use the below, it doesn't work :
src="{{asset('user/uploads/avatars/{ Auth::user()->avatar}') }}"
Also just to mention, the below is working fine for some pages but others not. Thats why I decided to use the asset() helper.
src="user/uploads/avatars/{{ Auth::user()->avatar }}"
I hope this helpful you
src="{{asset('user/uploads/avatars/'.Auth::user()->avatar) }}"
Try it with excluding Auth::user()->avatar from the string.
<img src="{{ asset('user/uploads/avatars/'". Auth::user()->avatar .") }}">

Correct link to storage in laravel

I want to display avatar in my page but I do not know how to do it. Look, here's my script. It doesn't work. Can you help me?
#foreach($user->user_data as $data)
<img alt="" src="{{ asset('/storage/uploads/avatars/ {{ $data->avatar }} ') }}">
#endforeach
There is a storage_path helper in laravel. Try using this:
#foreach($user->user_data as $data)
<img alt="" src="{{ storage_path('uploads/avatars/' . $data->avatar) }}">
#endforeach
Link to the doc: https://laravel.com/docs/5.5/helpers#method-storage-path
If you have installed Laravel the normal way you can't link to the storage folder directly because it's located outside of the public folder.
Only the contents of the public folder are accessible to the outside world.
There are two ways to work around this limitation, depending on how sensitive the stored data is.
If anyone may access the file, the easiest solution is to create a symlink from inside your public folder to the avatar storage folder.
If the file needs to be protected, create a Controller class that authorises the user and then returns the desired data from the storage_path.
In your case it sounds like solution 1 would suffice, and fortunately for you Laravel has built-in support for this.
By executing the following command in your command line Laravel will create a symbolic link from public/storage to storage/public:
php artisan storage:link
You can then create a link to anything that is stored in storage/public by using the asset function
asset('storage/path_relative_to_storage_public')
For a more detailed explanation, see: https://laravel.com/docs/5.5/filesystem#the-public-disk
In Laravel, code inside {{ }} will be interpreted as php code, You can Try this One :
#foreach($user->user_data as $data)
<img alt="" src="{{ asset('/storage/uploads/avatars/'. $data->avatar) }}">
#endforeach
It's like you do:
<img alt="" src="<?php echo asset('/storage/uploads/avatars/'. $data->avatar); ?>">

Laravel doesn't show image but url is correct

I want to get an image that is stored like this: public/pics/products.
Code:
<img src="{{ $shopGSM->productshot->first()->image }}" id="webshoppic">
The result:
<img src="pics/products/d132da08c7c01659edad47142261301b.jpg" id="webshoppic">
But no picture :(, what do i do wrong?
You didn't include the site url in src attribute. use following code
src="{{ URL::asset($shopGSM->productshot->first()->image)}}"
This will return something like as follow
src="http://www.domainName.com/pics/products/d132da08c7c01659edad47142261301b.jpg"
Hope this will work for you

Laravel 4.1 blade linking to user profile picture

I am trying to link to a user profile image in Laravels blade template but I am only getting errors here.
This is my image tag containing the link:
<img class="img-circle dashboardprofileimage" src="{{ URL::asset('img/profile_pictures/users/{{ Auth::user()->profile_picture }}') }}"/>
I would be very happy if anyone could help me out here. I guess its a simple thing but I have tried quite a lot of times now.
Thanks.
Your error is using nested {{ }}, you just need it once. Check out the correct code below:
<img
class="img-circle dashboardprofileimage"
src="{{
URL::asset('img/profile_pictures/users/' . Auth::user()->profile_picture)
}}" />
Note: it's splited into several lines for better legibility.
you just use {{ }} one time to print URL::asset() and Auth::user()
<img class="img-circle dashboardprofileimage" src="{{ URL::asset('img/profile_pictures/users/'.Auth::user()->profile_picture) }}"/>

Categories