custom pagination in laravel code example
Example 1: laravel pagination
{{ $users->withQueryString()->links() }}
Example 2: create custom pagination in laravel 7 for api
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
class PaginationController extends Controller
{
public function index()
{
$myArray = [
['id'=>1, 'title'=>'Laravel 6 CRUD'],
['id'=>2, 'title'=>'Laravel 6 Ajax CRUD'],
['id'=>3, 'title'=>'Laravel 6 CORS Middleware'],
['id'=>4, 'title'=>'Laravel 6 Autocomplete'],
['id'=>5, 'title'=>'Laravel 6 Image Upload'],
['id'=>6, 'title'=>'Laravel 6 Ajax Request'],
['id'=>7, 'title'=>'Laravel 6 Multiple Image Upload'],
['id'=>8, 'title'=>'Laravel 6 Ckeditor'],
['id'=>9, 'title'=>'Laravel 6 Rest API'],
['id'=>10, 'title'=>'Laravel 6 Pagination'],
];
$myCollectionObj = collect($myArray);
$data = $this->paginate($myCollectionObj);
return view('paginate', compact('data'));
}
public function paginate($items, $perPage = 5, $page = null, $options = [])
{
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}
}
<div class="container">
<table class="table table-bordered">
<tr>
<th>Id</th>
<th>Title</th>
</tr>
@foreach($data as $post)
<tr>
<td>{{ $post->id }}</td>
<td>{{ $post->title }}</td>
</tr>
@endforeach
</table>
</div>
{{ $data->links() }}
Example 3: laravel pagination
DB::table('users') -> paginate(15)
Example 4: custom pagination laravel css
php artisan vendor:publish
OR
use Illuminate\Pagination\Paginator;
public function boot()
{
Paginator::defaultView('your-pagination-view-name');
}
Example 5: Laravel Pagination
@if (isset($paginator) && $paginator->lastPage() > 1)
<ul class="pagination">
<?php
$interval = isset($interval) ? abs(intval($interval)) : 3 ;
$from = $paginator->currentPage() - $interval;
if($from < 1){
$from = 1;
}
$to = $paginator->currentPage() + $interval;
if($to > $paginator->lastPage()){
$to = $paginator->lastPage();
}
?>
<!
@if($paginator->currentPage() > 1)
<li>
<a href="{{ $paginator->url(1) }}" aria-label="First">
<span aria-hidden="true">«</span>
</a>
</li>
<li>
<a href="{{ $paginator->url($paginator->currentPage() - 1) }}" aria-label="Previous">
<span aria-hidden="true">‹</span>
</a>
</li>
@endif
<!
@for($i = $from; $i <= $to; $i++)
<?php
$isCurrentPage = $paginator->currentPage() == $i;
?>
<li class="{{ $isCurrentPage ? 'active' : '' }}">
<a href="{{ !$isCurrentPage ? $paginator->url($i) : '#' }}">
{{ $i }}
</a>
</li>
@endfor
<!
@if($paginator->currentPage() < $paginator->lastPage())
<li>
<a href="{{ $paginator->url($paginator->currentPage() + 1) }}" aria-label="Next">
<span aria-hidden="true">›</span>
</a>
</li>
<li>
<a href="{{ $paginator->url($paginator->lastpage()) }}" aria-label="Last">
<span aria-hidden="true">»</span>
</a>
</li>
@endif
</ul>
@endif