How to make an asset to show dynamically in Laravel - php

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 .") }}">

Related

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.

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

Img not displaying correctly in Symfony

My twig file goes like this :
Let us try to see an image :
<img src="{{ absolute_url(asset('app/Resources/images/bulb.png')) }}" alt="Symfony!" width="42" height="42"/>
Trying it another way :
<img src="app/Resources/images/bulb.png" alt="Symfony!" width="42" height="42"/>
But when I go to that page in Symfony, I see something like this :
What did I do wrong ?
currently, twig is going to be trying to find your asset in:
/web/app/Resources/images/bulb.png
use instead:
{{ asset('#AppBundle/Resources/public/images/bulb.png', absolute=true) }}
note the use of an additional public folder. If you must store assets in app, then this is sensible.
However, #Ewan Delanoy is correct, you really should be storing all your assets directly in the web folder.
Then you can just call
{{ asset('images/bulb.png', absolute=true) }}

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) }}"/>

Is there any easy way to set image asset in symfony 2

I have the html template where i image is referenced as images/logo.gif
Now in Symfony templates i have to use
src="{{ asset(['images/', 'logo.gif']|join) }}"
Is it possible to use something like
src="{{ asset(['images'])}}/logo.gif
so that i need not replace all the image tags in html file. Then i can find and replace easily. Othwise i have to manually change all image occurances
Why don't you just use this?
src="{{ asset('images/logo.gif') }}"
EDIT:
I didn't try it but concatenation should work as well:
{% set imageDir = 'images/' %}
src="{{ asset(imageDir ~ 'logo.gif') }}"
However, it introduces unnecessary complexity. I don't see anything wrong in a hard coded version. You don't change it often.

Categories