1
votes

I have a Laravel application. One of the pages can be reached via the following URL

http://localhost:8000/items/gallery?item_type=glasses

As the amount of items to be shown can be quite substantial, I'm using pagination. I have the following code in my view:

@foreach($media as $media_item)
   <div class="col-md-3">
       <div class="card">
          <img class="card-img-top" src="{{ asset('storage/'.$media_item->id .'/'. $media_item->file_name)  }}" ">
       </div>
   </div>
@endforeach
{{ $media->links() }}

and in the controller, I'm using:

$media = Media::paginate(5);

The pagination buttons are shown and work for the 1st one. Then when I click on the second (or third or fourth...) one, I get the following error message:

Method Illuminate\Database\Eloquent\Collection::links does not exist.

I see the link is trying to reach:

http://localhost:8000/beeritems/gallery?page=2

whereas I need:

http://localhost:8000/beeritems/gallery?item_type=glasses&page=2

In Laravel, how can I change the links() method to include the part after the question mark?

2
can you show your controller whole method?Davit

2 Answers

1
votes

You must use ->appends() methods

$media = Media::paginate(5);
$media->appends($request->all());
1
votes

you can use laravel basic URLs instead of getting gallery images with URL get parameters. something like this:

define Route like this

/items/gallery/{types}

then using it like

http://localhost:8000/items/gallery/glasses

in this case you don't get that error anymore