我试图将数据库中的所有数据显示到index.blade.php中,但是有一个错误显示Undefined Offset1。我使用了数组。我的表格表格中没有显示任何数据。这是我的代码。请向我介绍Laravel,谢谢。
我的查看 出现问题的地方
<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');
}
请为我提供所有指导,谢谢
答案 0 :(得分:0)
您的foreach类型将其更改为此错误一些
<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 as $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>
foreach
的第一个选项是要循环通过的变量,第二个选项是每个循环的变量