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

5sxhfpxr  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(390)

我试图显示数据库中的所有数据到我的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');
}

请引导我完成这一切谢谢

bcs8qyzn

bcs8qyzn1#

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

相关问题