Laravel-在我的数据库的一个ROW中存储的视图中显示我的阵列图像

时间:2018-11-25 04:30:23

标签: php html mysql arrays laravel

enter image description here我想在一个ROW中显示数据库中显示的所有图像。图像存储在我的数据库中的一个数组中,因此位于一个ROW中。目前,在我的代码中,该图片未显示,并且无法显示

这是我的 CONTROLLER

存储数据

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


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

这是我的 VIEW

 @foreach($promotions as $promotion)
           <tr>  

            <th><img src="{{ asset('storage/promotion_images/' . $promotion->promotion_image) }}" style="width:50px;height:50px;"></th>
 @endforeach

2 个答案:

答案 0 :(得分:0)

if (count($promotion)) {
        $implodedPromotion = implode(' , ', $promotion);
        $promotionImage = new Promotion;
        $promotionImage->promotion_image = $implodedPromotion;
        $promotionImage->save();

        return redirect('/admin/airlineplus/promotions',$promotion)->with('success', 'Image Inserted');
    }

    return redirect('/admin/airlineplus/promotions',$promotion)->with('error', 'Something went wrong.');

答案 1 :(得分:0)

您所做的所有存储图像工作似乎都还不错,您要做的就是将图像数据存储在数据库中。因此,像这样更改您的视图代码:

@foreach($promotions as $promotion)
       <tr>  

        <th>
            @foreach(explode(',' ,$promotion->promotion_image) as $image)
                <img src="{{ asset('storage/promotion_images/' . $image) }}" style="width:50px;height:50px;"><br/>
            @endforeach
        </th>
     </tr>
@endforeach

我认为这对您有用。如果您对以上代码有任何疑问,请随时询问。