laravel 如何使用laravel刀片添加动态图像src

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23712910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:48:43  来源:igfitidea点击:

how to add a dynamic image src with laravels blade

laravelblade

提问by Sven van den Boogaart

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

我的问题很简单。我在 Laravel 中使用刀片来显示图像

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

如何在刀片 {{}} 调用中调用 $variable?

回答by Razor

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

asset('assets/images/')相当于asset('assets/images')所以你需要/在打印变量之前添加$imgsrc

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

最后,在 Blade{{"foo"}}中相当于<?php echo "foo" ?>

回答by Laurence

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

如何在刀片 {{}} 调用中调用 $variable?

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

正常的 PHP 方式。刀片{{}}只是等价物<?php echo() ?>

So the answer to your question is:

所以你的问题的答案是:

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