从控制器到视图的 Laravel 图像路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36400876/
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
Laravel image paths from controller to view
提问by Suganya Rajasekar
My image's path is public/uploads/users/image.png
then using src
like this:
我的图像路径public/uploads/users/image.png
然后使用src
如下:
<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
在这里,我必须$user_img
根据控制器更改用户图像
How should I give it in the src
of image tag.
我应该如何在src
图像标签中给出它。
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.
来人帮帮我。
回答by Ben Fransen
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..)
如果这不起作用,则问题不在于资产助手功能,而可能在于值$user_img
获取(或不获取..)
回答by Shrikant Bhardwaj
You can use this method for image in laravel.
您可以将此方法用于 Laravel 中的图像。
if this is your image path public/assets/uploads/users/image.png.
如果这是您的图像路径public/assets/uploads/users/image.png。
image in $user_img
在图像$ user_img
{{asset('assets/uploads/users').'/'.$user_img}}
Or we can use
或者我们可以使用
{{ asset('assets/uploads/users') }}/{{$user_img}}
回答by Abderhman Shallan
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
其他答案.... 是 scsesfual
回答by Md Mahfuzur Rahman
If you have laravelcollective/html
, try this:
如果你有laravelcollective/html
,试试这个:
{{ Html::image('public/uploads/users/'.$user_img) }}
By the way, did you pass the $user_img
to the view
from controller
?
顺便说一句,你把 the 传递$user_img
给view
from 了controller
吗?
回答by Suganya Rajasekar
Thank you all,
谢谢你们,
I can't understand why its not working for me.
我不明白为什么它不适合我。
But the $user_img
retrieve the exact image name.
但是$user_img
检索确切的图像名称。
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
在我看来,我只是$user_img
在src
src="{{$user_img}}"
Edit
编辑
By using Shrikant Bhardwajcode I gave it as
通过使用 Shrikant Bhardwaj代码,我将其作为
{{asset('uploads/users').'/'.$user_img}}
Thank you. :)
谢谢你。:)
回答by John Doe
try
尝试
src="{{ asset(\'uploads/users/$user_img\')}}"
回答by Ankit Sompura
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
使用 Laravel Collective HTML 包安装此包后,您可以像这样使用
{!! Html::style('your/css/file.css') !!}
{!! Html::script('your/js/file.js') !!}
{!! Html::image('your/image/file.jpg') !!}