Laravel implode 图像数组函数

Laravel implode function on images array

如何使用图像数组将其变成内爆函数?我收到以下错误:

implode(): Invalid arguments passed

我的控制器:

    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