Php 无法显示数据库中的图像-Laravel
我与产品和图像有关系。用户可以上传一个产品的许多图片,所以我有一个产品表和产品图片表 当我试图从数据库中获取图像时,我的视图中没有可用的图像。但是我可以保存到数据库中的path属性Php 无法显示数据库中的图像-Laravel,php,laravel,Php,Laravel,我与产品和图像有关系。用户可以上传一个产品的许多图片,所以我有一个产品表和产品图片表 当我试图从数据库中获取图像时,我的视图中没有可用的图像。但是我可以保存到数据库中的path属性 $request->file('image')->store('uploads/catalog/images'); // ensure every image has a different name $file_name = $request->file('image')->hashName
$request->file('image')->store('uploads/catalog/images');
// ensure every image has a different name
$file_name = $request->file('image')->hashName();
// save new image $file_name to database
$product_image->images()->update(['image' => $file_name]);
为什么会发生这种情况
产品图片
id
product_id
path
产品
public function images()
{
return $this->hasMany(Image::class);
}
图像
public function products()
{
return $this->belongsTo(Product::class);
}
控制器
public function index()
{
$products = Product::where('type', 'BASIC')->inRandomOrder()->take(6)->get();
return view('home.index',compact('products');
}
$product_image = Product::create($request->all());
if ($request->hasFile('image'))
{
$request->file('image')->store('uploads/catalog/images');
// ensure every image has a different name
$file_name = $request->file('image')->hashName();
// save new image $file_name to database
$product_image->images()->update(['image' => $file_name]);
}
查看
@foreach($products as $product)
@foreach($product->image as $img)
<div class="product-item men">
<div class="product discount product_filter">
<div class="product_image">
<a href="{{ route('product.view', $product->slug)}}"><img src="{{$img}}" alt=""></a>
</div>
<!-- <div class="favorite favorite_left"></div> -->
<!-- <div class="product_bubble product_bubble_right product_bubble_red d-flex flex-column align-items-center"><span>offer</span></div> -->
<div class="product_info">
<h6 class="product_name"><a href="{{ route('product.view', $product->slug)}}">{{$product->name}}</a></h6>
</div>
</div>
</div>
@endforeach
@endforeach
如果您的图像位于
public
文件夹中,请使用asset()
这里有一个例子
<img src="{{ asset(str_replace(app_path(),'',$img->path))}}" alt="">
如果是存储文件夹,则将图像移动到存储,然后使用asset()或创建符号链接
使用asset(str\u replace(app\u path(),“”,$img->path))
希望这有帮助您不能使用
img
对象来回显它<代码>图像必须具有一些属性,如url
或路径
,因此只能输出它们。看起来您在path
属性中存储了完整路径(为什么?)
$url = str_replace(public_path(), "", $img->path);
<img src="{{ $url }}" alt="">
$url=str\u replace(public\u path(),“”,$img->path);
如果您将文件名存储为图像属性,那么请获取指向它的url
<img src="{{ Storage::url($img->path_to_file) }}" alt="">
path\u to\u file)}“alt=”“>
您的问题在于如何在数据库中存储图像
$request->file('image')->store('uploads/catalog/images');
// ensure every image has a different name
$file_name = $request->file('image')->hashName();
// save new image $file_name to database
$product_image->images()->update(['image' => $file_name]);
您使用store()
保存文件,然后使用hashName()
生成一个新文件名,这与存储在'uploads/catalog/images'
中的图像文件没有任何关系
正确的方法是获取store()
方法返回的存储图像的完整路径,并将其保存在数据库中
$image_path = $request->file('image')->store('uploads/catalog/images');
$product_image->images()->update(['image' => $image_path]);
$product->image
在您的@foreach
关系中可能应该是$product->images
,在您的关系中,您有$product->images,在您的foreach中您正在做$product->images,应该是$product->images和{{{$img->path}}@rchatburn,我得到这个错误htmlspecialchars()期望参数1是字符串,对象给定
@kerbholz,我得到的错误如comentdd($img)中所示,并发布结果src=“{asset($img)}}”
仍然给出图像不可用,但当我做src=“{asset($img->path)}”时,我得到这个错误preg_match()期望参数2是字符串,对象给定
@foreach($product->image as$img)@XamarinDevil您缺少@foreach($product->image as$img)@XamarinDevil use$product->images@XamarinDevil对不起,我的建议是,您最好先学习html。您的dd
根本不是dd
:)如何存储图像文件内容?为什么需要回显它?看起来您调用了dd()
在img标签内:)为什么?!)我已经更新了我的dd()结果,并检查了我的控制器如何存储我的文件content@XamarinDevil检查更新的答案(第一部分)
$image_path = $request->file('image')->store('uploads/catalog/images');
$product_image->images()->update(['image' => $image_path]);