duanan1946 2018-11-25 04:30
浏览 180
已采纳

Laravel - 在视图中显示我的数组图像,该图像存储在我的数据库中的一个ROW中

enter image description hereI want to display all of my images that is displayed on my database in one ROW. The images are stores in an array in my database so it is in one ROW. Currently in my code the image is not showing and it is UNAVAILABLE

Here is my CONTROLLER

Storing Data

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

Here is my VIEW

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

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

2条回答 默认 最新

  • dpqmu84646 2018-11-25 05:20
    关注

    All you have done for storing images seems okay, what you have to do is explode the image data store in the database. So, change your view code like this:

    @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
    

    I think this will work for you. If you have any confusion regarding above code, feel free to ask.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 Ubuntu开机显示器只显示kernel,是没操作系统(相关搜索:显卡驱动)
  • ¥15 VB.NET如何绘制倾斜的椭圆
  • ¥15 arbotix没有/cmd_vel话题
  • ¥20 找能定制Python脚本的
  • ¥15 odoo17的分包重新供应路线如何设置?可从销售订单中实时直接触发采购订单或相关单据
  • ¥15 用C语言怎么判断字符串的输入是否符合设定?
  • ¥15 通信专业本科生论文选这两个哪个方向好研究呀