【问题标题】:Display Multiple Images in Laravel Datatables在 Laravel 数据表中显示多个图像
【发布时间】:2021-07-19 04:30:39
【问题描述】:

我多次尝试在数据表中显示多个图像,但总是失败。它只显示单个图像。

数据库中的图像

[{"name":"_MG_2288 (1).jpg","path":"public\/product_images\/\/mKBWAFw7ZYgVqvz1lrxrEA5PYbal1EqqKm9jEx0F.jpg"},{"name":"555dca050423bda8418b4567.jpeg","path":"public\/product_images\/\/QnYnO9SfA9JJIu7wm0rxJOIJog6txmED8pdiZ4nM.jpg"}]

控制器

$data = \App\Product::latest()->get();

        return Datatables::of($data)
            ->addIndexColumn()
            ->addColumn('photo', function ($product) {
                foreach (json_decode($product->product_photo) as $picture) {
                    $pict = '<img src="/reference/eureka/storage/app/' . $picture->path . '" style="height:120px; width:200px"/>';
                    return $pict;
                }
            })
            ->rawColumns(['photo'])
            ->make(true);

查看

{
                data: 'photo',
                name: 'photo',
                render: function(data, type, full, meta) {
                    return data;
                }
            },

【问题讨论】:

    标签: jquery ajax laravel laravel-5 datatables


    【解决方案1】:

    在控制器中保存这样

            if($request->hasfile('product_image'))
            {
    
                foreach($request->file('product_image') as $product_image)
                {
                    $name=$product_image->getClientOriginalName();
                    $destinationPath = public_path('/uploads/products/');
                    $product_image->move($destinationPath, $name);
    
                    $product_images[] = $name;
                }
    
                $post->product_image = $product_images;
    
            }
    

    如果在轮播中返回,则在视图中使用滑块并获取带有索引的图像

                <div class="w3-white w3-text-grey w3-card-4" >
    
                    <div id="carouselExampleFade" class="carousel slide carousel-fade" data-ride="carousel">
                        <div class="carousel-inner">
    
    
                            @foreach($data->product_image as $slider)
    
                                <div class="carousel-item {{ $loop->first ? 'active' : '' }}" width="250" height="300">
    
                                    <img class="d-block w-100" src="{{asset('uploads/products/' . $slider)}}" width="250" height="300" alt="{{$data->product_name}}">
    
                                </div>
    
                            @endforeach
    
    
                        </div>
                        <a class="carousel-control-prev" href="#carouselExampleFade" role="button" data-slide="prev">
                            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                            <span class="sr-only">Previous</span>
                        </a>
                        <a class="carousel-control-next" href="#carouselExampleFade" role="button" data-slide="next">
                            <span class="carousel-control-next-icon" aria-hidden="true"></span>
                            <span class="sr-only">Next</span>
                        </a>
                    </div>
    
                    <div class="w3-container" style="text-align: center">
                    </div>
                </div><br>
    
    

    【讨论】:

    • 这是我将所有图像显示为视图的方式,但一次显示一个...我不知道您是否想像画廊一样展示它们,但对于一个时间,效果很好。
    猜你喜欢
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 2015-04-18
    • 2020-11-16
    • 2021-09-08
    相关资源
    最近更新 更多