gpt4 book ai didi

php - Laravel 将我的数据数组显示到 index.blade.php - 未定义偏移 1

转载 作者:行者123 更新时间:2023-11-29 16:39:23 24 4
gpt4 key购买 nike

我试图将数据库中的所有数据显示到我的index.blade.php,但是,有一个错误显示 undefined offset 1。我使用了数组。我的表格中没有显示任何数据。这是我的代码。请指导我,我是 Laravel 新手,谢谢。

My VIEW Here is where the problem occurs

 <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>

My CONTROLLER INDEX FUNCTION

public function index()
{
$promotions = Promotion::all();
return view('admin.airlineplus.promotions.index')->with('promotions', $promotions);
}

My CONTROLLER STORE FUNCTION
Here is where I store my data using arrays

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');
}

请指导我完成所有这一切,谢谢

最佳答案

您在将 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 第一个选项是要循环的变量,第二个选项是每个循环的变量

http://php.net/manual/en/control-structures.foreach.php

关于php - Laravel 将我的数据数组显示到 index.blade.php - 未定义偏移 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53422254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com