在 Laravel 数据表中显示多个图像
Posted
技术标签:
【中文标题】在 Laravel 数据表中显示多个图像【英文标题】:Display Multiple Images in Laravel Datatables 【发布时间】:2021-07-19 04:30:39 【问题描述】:我多次尝试在数据表中显示多个图像,但总是失败。它只显示单个图像。
数据库中的图像
["name":"_MG_2288 (1).jpg","path":"public\/product_images\/\/mKBWAFw7ZYgVqvz1lrxrEA5PYbal1EqqKm9jEx0F.jpg","name":"555dca050423bda8418b4567.jpeg","path":"public\/product_images\/\/QnYnO9SfA9JJIu7wm0rxJOIJog6txmED8pdiZ4nM.jpg"]
控制器
$data = \App\Product::latest()->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('photo', function ($product)
foreach (json_decode($product->product_photo) as $picture)
$pict = '<img src="/reference/eureka/storage/app/' . $picture->path . '" style="height:120px; width:200px"/>';
return $pict;
)
->rawColumns(['photo'])
->make(true);
查看
data: 'photo',
name: 'photo',
render: function(data, type, full, meta)
return data;
,
【问题讨论】:
【参考方案1】:在控制器中保存这样
if($request->hasfile('product_image'))
foreach($request->file('product_image') as $product_image)
$name=$product_image->getClientOriginalName();
$destinationPath = public_path('/uploads/products/');
$product_image->move($destinationPath, $name);
$product_images[] = $name;
$post->product_image = $product_images;
如果在轮播中返回,则在视图中使用滑块并获取带有索引的图像
<div class="w3-white w3-text-grey w3-card-4" >
<div id="carouselExampleFade" class="carousel slide carousel-fade" data-ride="carousel">
<div class="carousel-inner">
@foreach($data->product_image as $slider)
<div class="carousel-item $loop->first ? 'active' : '' " >
<img class="d-block w-100" src="asset('uploads/products/' . $slider)" >
</div>
@endforeach
</div>
<a class="carousel-control-prev" href="#carouselExampleFade" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleFade" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<div class="w3-container" style="text-align: center">
</div>
</div><br>
【讨论】:
这是我将所有图像显示为视图的方式,但一次显示一个...我不知道您是否想像画廊一样展示它们,但对于一个时间,效果很好。以上是关于在 Laravel 数据表中显示多个图像的主要内容,如果未能解决你的问题,请参考以下文章