mysql - 图像数组上的 Laravel 内爆函数
问题描述
如何使用图像数组使其成为内爆函数?我收到以下错误:
implode():传递的参数无效
我的控制器:
public function store(Request $request)
{
$this->validate($request, [
'promotion_image' => 'required'
]);
if ($request->has('promotion_image'))
{
//Handle File Upload
$promotion = [];
foreach ($request->file('promotion_image') as $key => $file)
{
// Get FileName
$filenameWithExt = $file->getClientOriginalName();
//Get just filename
$filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
//Get just extension
$extension = $file->getClientOriginalExtension();
//Filename to Store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
//Upload Image
$path = $file->storeAs('public/promotion_images',$fileNameToStore);
array_push($promotion, $fileNameToStore);
}
$fileNameToStore = serialize($promotion);
}
else
{
$fileNameToStore='noimage.jpg';
}
foreach ($promotion as $key => $value) {
$promotionImage = new Promotion;
$promotionImage->promotion_image = implode(' , ',$value);
$promotionImage->save();
}
return redirect('/admin/airlineplus/promotions')->with('success', 'Image Inserted');
}
我的观点:
@foreach($promotions as $promotion)
<tr>
//HERE IS WHERE THE IMAGE ARE VIEWED <th><img src="{{ asset('storage/promotion_images/' . $promotion->promotion_image) }}" style="width:50px;height:50px;"></th>
<th><a href="/admin/airlineplus/promotions/{{ $promotion->id }}/edit" class="fa fa-edit btn btn-primary btn-lg"></a></th>
</tr>
@endforeach
解决方案
如果我说对了,请尝试不使用 foreach 循环
if (count($promotion)) {
$implodedPromotion = implode(' , ', $promotion);
$promotionImage = new Promotion;
$promotionImage->promotion_image = $implodedPromotion;
$promotionImage->save();
return redirect('/admin/airlineplus/promotions')->with('success', 'Image Inserted');
}
return redirect('/admin/airlineplus/promotions')->with('error', 'Something went wrong.');
补充:
如果您的视图中有该值并希望显示这些图像,则应该执行以下操作:
@foreach($promotions as $promotion)
@php
$imagesImploded = $promotion->promotion_image;
$imagesExploded = explode(',', $imagesImploded);
@endphp
<tr>
@foreach($imagesExploded as $image)
<th><img src="{{ asset('storage/promotion_images/' . trim($image)) }}" style="width:50px;height:50px;"></th>
@endforeach
<th><a href="/admin/airlineplus/promotions/{{ $promotion->id }}/edit" class="fa fa-edit btn btn-primary btn-lg"></a></th>
</tr>
@endforeach
推荐阅读
- .net - 我应该在 HTTPResponse 中返回长诊断消息的哪个属性?
- android - 在 SearchView 中覆盖 BackButton 按下
- azure-digital-twins - Azure 数字孪生 UDF 获取空间位置
- neo4j - 如何在 Cypher 中按降序排列“空”结果?
- python - 为什么当我将数据添加到我的数据框列时我的数据是空的?
- ios - 在 iOS 12 中阻止调用崩溃
- autodesk-forge - Autodesk Forge 停靠面板水平滚动
- php - @提及系统不会产生正确的输出
- c# - 在c#中查询特定值von Enum List
- dockerfile - -o /publish/' 返回一个非零代码:1