laravel内爆函数

e4eetjau  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(274)

如何使用图像数组使其成为内爆函数?我得到一个错误:
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
zfciruhq

zfciruhq1#

如果我没弄错的话,试着不用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

相关问题