how to add a dynamic image src with laravels blade

心已入冬 提交于 2019-12-04 21:36:49

问题


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?


回答1:


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" ?>




回答2:


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


来源:https://stackoverflow.com/questions/23712910/how-to-add-a-dynamic-image-src-with-laravels-blade

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!