Laravel在图像阵列上内爆功能

时间:2018-11-25 01:14:14

标签: mysql arrays laravel implode

如何使用图像数组使它成为爆破功能?我收到以下错误:

  

implode():传递了无效的参数

我的控制器:

    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

1 个答案:

答案 0 :(得分:0)

如果我说对了,请尝试不使用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