1

I'm working on a old PHP project, that is running in legacy SQL query, which is good but I like to use query builders like Laravel Illuminate SQL package!

So i have added all required package dependencies to run Illuminate SQL, and this query builder seems to work fine with pagination!

$users = Capsule::table('users')->paginate(1)->toArray();

But, the paginator seems not to be able to listen the query string! For example, by running the above code it would give some properties like, next_page , previous_page etc...

enter image description here

And when I try to pass the query string in the URL it can't fetch the data from query string(From the GET request)!

Visiting this page http://app.app/?page=2 would give the same result set.

How i should configure the Illuminate sql package so it can also listen to the query strings?

EDIT

Also, i've tried to use the illuminate/http package, but the $request->all() method always returns an empty array! Here is the code:

<?php
  require_once './vendor/autoload.php';
  use \Illuminate\Http\Request;

  $req = new Request();

  echo '<pre>';
  print_r($req->all());
  echo '</pre>';

It returns empty input data array, enter image description here

What i am missing to use the http package, any idea would be helpful.

rakibtg
  • 5,521
  • 11
  • 50
  • 73

3 Answers3

4

You have to set a page resolver:

\Illuminate\Pagination\Paginator::currentPageResolver(function ($pageName = 'page') {
    return (int) ($_GET[$pageName] ?? 1);
});

Laravel uses this resolver.

Jonas Staudenmeir
  • 24,815
  • 6
  • 63
  • 109
1

You need to notice, that paginator won't use other parameters for query string - it just uses page parameter to get valid results page. But for example if you use:

http://app.app/?page=2&price=2

it won't take from database results with price = 2 - it's your job to do this.

However the 2nd thing are urls generated by paginator.

If you do it like this:

$users = Capsule::table('users')->paginate(1);

You can also use in next line

$users->appends(request()->except('page'));

This will add all other parameters from query string (except page) to urls, so first_page_url will then contain all other parameters from request.

You can also wrap it using fluent syntax like this:

$users = Capsule::table('users')->paginate(1)->appends(request()->except('page'));
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
0
  • You need to fetch the query string in the controller to pass the get parameter to paginate.

  • You can use the Illuminate\Http\Request class to receive and use the HTTP payload.

Your controller file:

<?php

use Illuminate\Http\Request;
class YourControllerClassName{
    public function someFunction(Request $request){
         echo "<pre>";
         print_r($request->all());
         print_r($request->input('page'));
         $users = Capsule::table('users')->paginate($request->input('page'))->toArray();
    }
}
nice_dev
  • 17,053
  • 2
  • 21
  • 35
  • I've tried this too; but for some reason the request->all returns empty array all the time – rakibtg Sep 18 '18 at 05:59
  • @rakibtg Why do you `autoload `on your own? Also, the reason you are getting an empty array is because you are creating an object of `Request` on your own. Make a controller file in `App\http\controllers`. Make a route in `routes/web.php`. Copy the above code and paste it in your controller. Save it and make a request from browser. You should see that it works. – nice_dev Sep 18 '18 at 06:25
  • I am trying to use this packages outside of the laravel framework, that's why i was instantiating the Request. – rakibtg Sep 18 '18 at 06:40
  • @rakibtg why so? If you want to use these packages outside of Laravel, then you don't need Laravel. You can have `composer.json` file in your project, mention the packages you want to have in `require` field and do a `composer install` and `composer dump-autoload` and you are ready to go. – nice_dev Sep 18 '18 at 07:14
  • can you give an example composer.json file, would be really helpful – rakibtg Sep 18 '18 at 10:10