【问题标题】:Laravel - Display my array images in view that is stored in one ROW in my DatabaseLaravel - 在视图中显示我的数组图像,这些图像存储在我的数据库中的一行中
【发布时间】:2019-04-27 03:02:39
【问题描述】:

我想在一行中显示我的数据库中显示的所有图像。这些图像存储在我数据库中的一个数组中,因此它位于一行中。目前在我的代码中,图像未显示,并且不可用

这是我的控制器

存储数据

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

这是我的视图

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

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

【问题讨论】:

  • 检查您的控制台。它将显示无法加载的图像 URL。根据该 URL,您需要修改图像路径。你能告诉你控制台错误吗?
  • 请查看新的编辑。我添加了问题..没有错误只是图像没有显示
  • @BanujanBalendrakumar 我编辑上面的问题请看图片..它没有错误,我的问题是图片没有显示
  • 该错误仅显示在控制台中。你应该检查你的路径。右键单击图像并单击Open Image in New Tab 然后显示图像URL,并显示目录树。
  • @BanujanBalendrakumar 我这样做了,据说找不到页面

标签: php html mysql arrays laravel


【解决方案1】:

您为存储图像所做的一切似乎还不错,您要做的就是在数据库中分解图像数据存储。所以,像这样改变你的视图代码:

@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

我认为这对你有用。如果您对上述代码有任何困惑,请随时提问。

【讨论】:

  • SO CLOSE ... 它存储 1 个值 .. 其他图像不可用,为什么会这样
  • @SummerWinter 能否请您更详细地描述一下,以便我可以帮助您
  • 我有一个使用 jquery 的动态表单,所有图像都存储在一个 ROW 中。你的代码只显示了 1 个图像我的数据库中有 2 个图像。所以另外 1 个图像是未显示在我的视图中。
  • @SummerWinter 请添加数据库表截图
  • 嘿先生你能回答我的这个问题吗link
【解决方案2】:
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.');

【讨论】:

  • 你能解释一下你的答案吗?
  • 那是什么?它似乎对我没有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-06
  • 1970-01-01
  • 2021-11-16
  • 2019-03-15
  • 1970-01-01
相关资源
最近更新 更多