0sudesh's avatar

Is there way to make pagination pretty url in Laravel 5.4

Is there a way to get a pagination pretty URL like this (http://localhost:8000/users/all/1). please help me.

http://localhost:8000/users/all?page=1 this is how it give me as default

Thanks.

0 likes
7 replies
jlrdw's avatar

Use parameters in routes instead of passing a querystring. Can be more coding involved, you have to keep track to make sure they are passed.

0sudesh's avatar

jlrdw, please can you explain more with an example

jlrdw's avatar

You would have to make a lengthaware paginator, and calc pages yourself is how I'd do it,

This was just a quick example I did a while back, convert to laravel request object

public function ownerlist()
    {
        if (isset($_REQUEST['t1'])) {
            $t1 = $_REQUEST['t1'];
        } else {
            $t1 = "";
        }

        if (isset($_REQUEST['page'])) {
            $page = $_REQUEST['page'];
        } else {
            $page = "1";
        }
        $perpage = "5";
        $offset = ($page - 1) * $perpage;

        $ownersearch = $t1;
        $ownersearch = $ownersearch . "%";
        $krows = DB::select('select COUNT(ownerid) as count from powners where oname like :oname', ["oname" => $ownersearch]);
        $numrows = $krows[0]->count;
        echo $numrows;

        $data = DB::table('powners')
                        ->where('oname', 'like', $ownersearch)
                        ->orderBy('oname', 'asc')
                        ->skip($offset)->take($perpage)->get();
        $pagelinks = LengthPager::makeLengthAware($data, $numrows, $perpage, ['t1' => $t1]);
        return view('owner/pownerlist')->with('data', $data)->with('data2', $pagelinks);
        
    }

That's not an seo example, in your case, you would have to keep track of the page you are on something like

public function yourFunction($page)

Of course if first load work it out to default to 1

And the class for the LengthPager

<?php namespace App\Services;

use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;

abstract class LengthPager
{

  /**
   * Create paginator
   *
   * @param  Illuminate\Support\Collection  $collection
   * @param  int     $total
   * @param  int     $perPage
   * @return string
   */
  public static function makeLengthAware($collection, $total, $perPage, $appends = null)
{
  $paginator = new LengthAwarePaginator(
    $collection, 
    $total, 
    $perPage, 
    Paginator::resolveCurrentPage(), 
    ['path' => Paginator::resolveCurrentPath()]
  );

  if($appends) $paginator->appends($appends);

  return str_replace('/?', '?', $paginator->render());
  //return str_replace('/?', '?', $paginator);
}

}//end class

This part would need tweaking for your "friendly url"

return str_replace('/?', '?', $paginator->render());

That you would need to work out as a developer, as I just use the querystring, default way of pagination.

1 like
Čamo's avatar

Ok I face the problem right now. Spatie package is abandoned now, so what library to use for pretty urls with page parameter?

Please or to participate in this conversation.