【发布时间】:2020-02-06 06:57:28
【问题描述】:
我正在使用 laravel 项目并遇到问题。 我创建了一个名为“Product”的数据库模型,它有一个名为“product_image”的字段,它的数据类型是字符串。
public function up()
{
Schema::create('Products', function (Blueprint $table) {
$table->string('product_image')->nullable();
$table->timestamps();
});
}
通过使用控制器,我确实启用了上传产品图像和产品模型,能够获取该文件的活动名称及其扩展名,即使我能够在 storage/app/example.jpg 上找到该上传的文件。
$file = $request->file('product_image');
$filename = 'product_image-' . time() . '.' . $file->getClientOriginalExtension();
$path = $file->storeAs('product-image', $filename);
$product->product_image = $path ;
但是当我在我的刀片模板上运行 @foreach 循环并尝试通过它获取图像时。
@foreach ($products as $p)
<tr>
<td>{{$p->product_title}}</td>
<td><img src="/storage/app/product-image/{{$p->product_image}}" alt="" style="height:80px; width:80px"/></td>
@endforeach
我什么都没有创建,图像字段仍然显示没有原始图像:( enter image description here
我发现了问题,但不知道解决方案,迷失在黑暗中。
实际问题在,<img src="/storage/app/product-image/{{$p->product_image}}" alt="" style="height:80px; width:80px"/>
因为它没有为该图像创建 url。
我的问题是,我们如何使用 for 循环为该特定图像提供 URL,这将能够从 storage/app/example 目录中找到该图像。 提前致谢。
【问题讨论】:
标签: php database laravel image url