我试图显示数据库中的所有数据到我的index.blade.php,但是,有一个错误显示未定义的偏移量1。我用数组。我的表格中没有显示任何数据。这是我的密码。请引导我,我是新来的,谢谢。
我的观点是问题出在这里
<div class="form-group">
<div class="table-responsive">
<table class="table table-bordered" id="dynamic_field">
<tr>
<th>Image Name</th>
<th>Action</th>
</tr>
@foreach($promotions->$promotion)
<tr>
<th>{{ $promotion->promotion_image }}</th>
<th><a href="" class="fa fa-edit">Update</a></th>
<th><a href="" class="">Remove</a></th>
</tr>
@endforeach
</table>
</div>
</div>
我的控制器索引函数
public function index()
{
$promotions = Promotion::all();
return view('admin.airlineplus.promotions.index')->with('promotions', $promotions);
}
我的控制器存储功能
这里是我使用数组存储数据的地方
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 = $value;
$promotionImage->save();
}
return redirect('/admin/airlineplus/promotions/create')->with('success', 'Image Inserted');
}
请引导我完成这一切谢谢
1条答案
按热度按时间bcs8qyzn1#
您将foreach类型更改为此类型时出现了一个小错误