Php 如何在LARAVEL中使用数据库查询访问存储文件
我在拉威尔项目工作,面临一个问题。 我创建了一个名为“Product”的数据库模型,其中有一个名为“Product_image”的字段,其数据类型为stringPhp 如何在LARAVEL中使用数据库查询访问存储文件,php,database,laravel,image,url,Php,Database,Laravel,Image,Url,我在拉威尔项目工作,面临一个问题。 我创建了一个名为“Product”的数据库模型,其中有一个名为“Product_image”的字段,其数据类型为string public function up() { Schema::create('Products', function (Blueprint $table) { $table->string('product_image')->nullable(); $t
public function up()
{
Schema::create('Products', function (Blueprint $table) {
$table->string('product_image')->nullable();
$table->timestamps();
});
}
通过使用controller,我确实能够上传产品图像和产品模型,从而能够获得该文件的活动名称及其扩展名,甚至我能够在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
@foreach($p形式的产品)
{{$p->product_title}
product_image}“alt=”“style=“高度:80px;宽度:80px“/>
@endforeach
我没有发现任何东西,图像场仍然显示,没有原始图像:(
我发现了问题,但不知道解决办法,迷失在黑暗中。
实际问题是,/storage/app/product image/{{{$p->product_image}}“alt=”“style=“height:80px;width:80px”/>
因为它没有为该图像创建url
我的问题是,我们如何使用for循环为特定图像提供URL,该循环将能够从storage/app/example目录中找到该图像。
提前感谢。您是否可以在浏览器中访问图像,如-
在您的问题中,您提到了storage/app/example.jpg,但在您的模板中访问时,您将其称为storage/app/product image/example.jpg。路径会是问题所在吗
您的循环看起来像OK.IMO。您是否尝试过使用artisan命令创建指向公共文件的符号链接,如所述
运行php artisan存储:链接
在config/filesystems.php
中,更改为如下内容:
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/product_image',
'visibility' => 'public',
],
],
然后访问刀片模板中的文件,如下所示:
<img src="{{ asset('product_image/'. $p->product_image) }}" alt="" style="height:80px; width:80px"/>
product_image)}“alt=”“style=“高度:80px;宽度:80px“/>
此外,如果您使用的是Vagrant或Laradock,则应在工作区内运行命令php artisan storage:link
,否则它将无法工作,在浏览器中访问时将导致404。查看路径,ok。