CosminAndrei's avatar

Display the uploaded images at the top of the gallery

I want to display the uploaded images at the top of the gallery but i don't know how to do that. There is a simple method which can I use it and which can be add to my code? I'm trying to find a solution because i have a gallery of images and I would like the images that are added to be displayed at the top of the gallery.

There is my controller :

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!');
}

And this is my view:

           @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
0 likes
4 replies
jlrdw's avatar

You should have a created at or updated at column. Order by that column, asc or desc depending on which type of order.

CosminAndrei's avatar

I would like the images that are added to be displayed at the top of the gallery. There is a solution to do that without sort the images?

gazd1977's avatar
gazd1977
Best Answer
Level 21

Do you mean the date the image is uploaded to the server (/added to the database) or are you trying to track down the date the photo was taken? I cant see any date fields in the set up, but maybe the default timestamps are part of the table set up?

Do you want the view to load with the images in order of date or do you want a filter on the page that will order them?

If you want the images to load in order (assuming date added to database) then as @jlrdw said then the easiest way is to add an orderBy to your database call (something like the below)

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

Finally yes! That i want to do. My images is stored in database and i want to load the images descending . This code work perfectly. Thanks a lot!

Please or to participate in this conversation.