【问题标题】:Display the uploaded images at the top of the gallery (Laravel)在图库顶部显示上传的图像(Laravel)
【发布时间】:2019-12-30 08:14:36
【问题描述】:

我想在图库顶部显示上传的图像,但我不知道该怎么做。有一个简单的方法可以使用它并且可以添加到我的代码中吗?我正在尝试寻找解决方案,因为我有一个图像库,我希望添加的图像显示在库的顶部。 有我的控制器:

public function index()
{
    $images_salons = SalonsImageGallery::get();
    return view('admin-panel.salons.salons', compact('images_salons'));
}
// * Upload image function

   public function upload(Request $request)
   {
    $this->validate($request, [
        'image_salons' => 'required',
        'image_salons.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:5000'
    ]);


    foreach ($request->image_salons as $image) {
        $input['image_salons'] = uniqid() . '.' . $image->getClientOriginalExtension();
        $destinationPath = public_path('/images/salons/thumbnails');
        $resize_image = Image::make($image->getRealPath());
        $resize_image->resize(500, 500, function($constraint){
            $constraint->aspectRatio();
        })->save($destinationPath . '/' .  $input['image_salons']);

        $destinationPath = public_path('/images/salons');
        $image->move($destinationPath, $input['image_salons']);

        SalonsImageGallery::create($input);
    }


    return back()
        ->with('success', 'Imaginea/imaginile au fost incarcate cu succes!');
}

这是我的观点:

@if($images_salons->count())

            @foreach($images_salons as $image)

                <div class='box'>
                    <a class="thumbnail fancybox" rel="ligthbox"
                       href="/images/salons/{{ $image->image_salons }}">
                        <img class="img-responsive" alt=""
                             src="/images/salons/thumbnails/{{ $image->image_salons }}"/>
                    </a>
                    <form action="{{ url('home/saloane',$image->id) }}" method="POST">
                        <input type="hidden" name="_method" value="delete">
                        {!! csrf_field() !!}
                        <button type="submit" class="close-icon btn btn-danger">X</button>
                    </form>
                </div>

            @endforeach

        @endif

【问题讨论】:

  • 如果您为图像编写文件名,您可以将其命名为 xyz_{current_timestamp}.jpg。比你可以按日期排序。或者你获取每张图片的exif数据:stackoverflow.com/questions/40685885/…
  • 嗯,不按日期排序图片可以在图库顶部显示上传的图片吗?

标签: php database laravel


【解决方案1】:

我有一个解决方案!

$images_salons = SalonsImageGallery::orderBy('created_at', 'desc')->get();

此代码显示我存储在数据库中的图像降序!

【讨论】:

  • 不知道,图片路径存储在数据库中。我以为你是直接从文件系统中获取的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-05
  • 1970-01-01
  • 1970-01-01
  • 2020-09-20
  • 1970-01-01
  • 2018-02-06
相关资源
最近更新 更多