1

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?

1
  • can you show your controller whole method? Commented Jun 3, 2019 at 18:09

2 Answers 2

2

You must use ->appends() methods

$media = Media::paginate(5);
$media->appends($request->all());
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Good suggestion, accepted answer works for now. Your suggestion requires quite some rework but definitely the better option. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.