how to add a dynamic image src with laravels blade
Asked Answered
V

3

10

My question is very simple. i use blade in laravel to show the images like

<img src="{{asset('assets/images/image.jpg')}}">

but what if my image is dynamic like:

<img src="{{asset('assets/images/$imgsrc')}}">

i cant use

 <img src="{{asset('assets/images/{{ $imgsrc')}}">

because then i will end with:

 <img src="http://localhost/assets/images/{{ $imgsrc">

How can i call to a $variable in a blade {{}} call?

Valenzuela answered 17/5, 2014 at 15:14 Comment(0)
O
21

How about

<img src="{{asset('assets/images/').'/'.$imgsrc}}">

EDIT

when you need printing more than one strings where there may be variables,functions... you need to concatenate them

Also asset('assets/images/') is equivalent to asset('assets/images') so you need to add / before printing your variable $imgsrc

Finally, in Blade {{"foo"}} is equivalent to <?php echo "foo" ?>

Overturf answered 17/5, 2014 at 15:23 Comment(1)
it removes the last slash assets/images/ becomes assets/imagestherighsrc.jpg (notice assets/images(/) is missingValenzuela
A
8

How can i call to a $variable in a blade {{}} call?

The normal PHP way. A blade {{}} is just the equilivant of <?php echo() ?>

So the answer to your question is:

<img src="{{ asset('assets/images/' . $imgsrc) }}">
Apical answered 17/5, 2014 at 16:4 Comment(1)
Your Answer is correct but {{}} is not equivalent of <?php ?>Tula
B
1

Mine had multiple dynamic parts in the src and this is how I did it after getting in the right direction, thanks to @Laurence's answer.

<img src="{{asset('storage/'.$parentFolderName.'/'. $clientNameFolder .'/logo/').'/'.$event[0]->event_logo}}" class="event-page-country-logo">
Bandaid answered 18/8, 2020 at 12:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.