php 如何在 Laravel 视图中添加图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36441679/
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
How do i add images in laravel view?
提问by Vishwanth Iron Heart
The thing is, my image is not directly present in my view
问题是,我的形象并没有直接出现在我的视野中
Route::Get('saakshar',function()
{
return view('version1');
});
and in my version1.blade.php
在我的 version1.blade.php 中
<?php
include(app_path()."/../resources/views/fronthead.blade.php");
?>
I know using php commands in blade file is not efficient or good in any manner, but blade command is not working, but that's not my main problem here.
我知道在刀片文件中使用 php 命令在任何方面都不高效或不好,但是刀片命令不起作用,但这不是我的主要问题。
and in my fronthead.blade.php
在我的 fronthead.blade.php 中
<img src='app_path()."/../resources/views/photos/logo.png"' alt="no logo">
I haven't learned fully the blade language, so for time being i am using php commands.
我还没有完全学习刀片语言,所以暂时我正在使用 php 命令。
But my problem is, why isn't the photo loading in the webpage? Is the URL wrong? Did I place the "photos" folder in the wrong place?
但我的问题是,为什么网页中的照片加载不出来?网址有错吗?我是否将“照片”文件夹放在了错误的位置?
Edit: I've even tried to place the photo is the same folder as fronthead.blade.php and changed the src tag.
编辑:我什至尝试将照片放置在与 fronthead.blade.php 相同的文件夹中,并更改了 src 标签。
I've even tried giving a separate Route::Get() from fronthead.blade.php, but still the problem persists.
我什至尝试从 fronthead.blade.php 中提供单独的 Route::Get(),但问题仍然存在。
回答by Alexey Mezenin
You should store your images, css and JS files in a public
directory. To create a link to any of them, use asset()
helper:
您应该将图像、css 和 JS 文件存储在一个public
目录中。要创建指向其中任何一个的链接,请使用asset()
helper:
{{ asset('img/myimage.png') }}
https://laravel.com/docs/5.1/helpers#method-asset
https://laravel.com/docs/5.1/helpers#method-asset
As alternative, you could use amazing Laravel Collectivepackage for building forms and HTML elements, so your code will look like this:
作为替代方案,您可以使用惊人的Laravel Collective包来构建表单和 HTML 元素,因此您的代码将如下所示:
{{ HTML::image('img/myimage.png', 'a picture') }}
回答by Peeje
<img src="/images/yourfile.png">
Store your files in public/images directory.
将您的文件存储在 public/images 目录中。
回答by tanvirjahan
If Image folder location is public/assets/img/default.jpg.
You can try in view
如果图像文件夹位置是public/assets/img/default.jpg.
您可以在视图中尝试
<img src="{{ URL::to('/assets/img/default.jpg') }}">
回答by channasmcs
normaly is better image store in public folder (because it has write permission already that you can use when I upload images to it)
normaly 是公共文件夹中更好的图像存储(因为它已经具有写入权限,您可以在我将图像上传到它时使用)
public
upload_media
photos
image.png
$image = public_path() . '/upload_media/photos/image.png'; // destination path
view PHP
查看 PHP
<img src="<?= $image ?>">
View blade
查看刀片
<img src="{{ $image }}">