I tested above after installing laravel 5.5, still works good. And boo boo in css, .pagination span.current should be .active and .pagination span.disabled can be just .disabled Sorry I posted the wrong css. But all works.
Jul 20, 2017
10
Level 75
Lengthaware paginator
When ver 5 came out @bestmomo helped me with Lengthaware code, I recently tested it on ver 5.4 it still works good:
- make a Services folder under app
- create a file called LengthPager.php
Code for 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());
}
}
Then at the top of controller
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Services\LengthPager;
use Illuminate\Http\Request;
A quick test method
public function indexTest(Request $request)
{
If (!empty($request->input('page'))) {
$page = $request->input('page');
} else {
$page = "1";
}
$perpage = "5";
$offset = ($page - 1) * $perpage;
$krows = DB::select('select COUNT(dogid) as count from dc_dogs where adopted = :adpt', ['adpt' => 0]);
$numrows = $krows[0]->count;
echo $numrows;
$t1 = "b"; // just test for querystring
$dogs = DB::table('dc_dogs')
->where('adopted', '=', 0)
->skip($offset)->take($perpage)->get();
$pagelinks = LengthPager::makeLengthAware($dogs, $numrows, $perpage, ['t1' => $t1]);
return view('dog/index')
->with('dogs', $dogs)
->with('pagelinks', $pagelinks);
}
And view
<?php
foreach ($dogs as $dog) {
echo $dog->dogname . "<br>";
}
echo $pagelinks;
At this point the page links will show vertical, they need style. Something like:
<style>
td, th {
border: solid 1px black;
}
.pagination li {display:inline; margin-right:5px; padding:3px;}
.pagination a {
border: 1px solid #D5D5D5;
color: #666666;
font-size: 11px;
font-weight: bold;
height: 25px;
padding: 4px 8px;
text-decoration: none;
margin:2px;
}
.pagination a:hover, .pagination a:active {
background:#efefef;
}
.pagination span.current {
background-color: #687282;
border: 1px solid #D5D5D5;
color: #ffffff;
font-size: 11px;
font-weight: bold;
height: 25px;
padding: 4px 8px;
text-decoration: none;
margin:2px;
}
.pagination span.disabled {
border: 1px solid #EEEEEE;
color: #DDDDDD;
margin: 2px;
padding: 2px 5px;
}
</style>
This works perfect in version 5.4 And of course the default app.css works fine.
Please or to participate in this conversation.
