paginate relationship laravel7
$category = Category::first();
$apps = $category->apps()->paginate(10);
return view('example', compact('category', 'apps'));
paginate relationship laravel7
$category = Category::first();
$apps = $category->apps()->paginate(10);
return view('example', compact('category', 'apps'));
laravel multiple paginate
# use default 'page' for this
$collection1 = Model::paginate(20);
# use custom 'other_page' for this
$collection2 = Model2::paginate(20);
$collection2->setPageName('other_page');
custom laravel pagination
@if ($paginator->hasPages())
<ul class="pager">
@if ($paginator->onFirstPage())
<li class="disabled"><span>← Previous</span></li>
@else
<li><a href="{{ $paginator->previousPageUrl() }}" rel="prev">← Previous</a></li>
@endif
@foreach ($elements as $element)
@if (is_string($element))
<li class="disabled"><span>{{ $element }}</span></li>
@endif
@if (is_array($element))
@foreach ($element as $page => $url)
@if ($page == $paginator->currentPage())
<li class="active my-active"><span>{{ $page }}</span></li>
@else
<li><a href="{{ $url }}">{{ $page }}</a></li>
@endif
@endforeach
@endif
@endforeach
@if ($paginator->hasMorePages())
<li><a href="{{ $paginator->nextPageUrl() }}" rel="next">Next →</a></li>
@else
<li class="disabled"><span>Next →</span></li>
@endif
</ul>
@endif
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
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
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'));
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
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);
}
}
# Blade file
<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() }}
Laravel custom pagination
<?php
// config
$link_limit = 7; // maximum number of links (a little bit inaccurate, but will be ok for now)
?>
@if ($paginator->lastPage() > 1)
<ul class="pagination">
<li class="{{ ($paginator->currentPage() == 1) ? ' disabled' : '' }}">
<a href="{{ $paginator->url(1) }}">First</a>
</li>
@for ($i = 1; $i <= $paginator->lastPage(); $i++)
<?php
$half_total_links = floor($link_limit / 2);
$from = $paginator->currentPage() - $half_total_links;
$to = $paginator->currentPage() + $half_total_links;
if ($paginator->currentPage() < $half_total_links) {
$to += $half_total_links - $paginator->currentPage();
}
if ($paginator->lastPage() - $paginator->currentPage() < $half_total_links) {
$from -= $half_total_links - ($paginator->lastPage() - $paginator->currentPage()) - 1;
}
?>
@if ($from < $i && $i < $to)
<li class="{{ ($paginator->currentPage() == $i) ? ' active' : '' }}">
<a href="{{ $paginator->url($i) }}">{{ $i }}</a>
</li>
@endif
@endfor
<li class="{{ ($paginator->currentPage() == $paginator->lastPage()) ? ' disabled' : '' }}">
<a href="{{ $paginator->url($paginator->lastPage()) }}">Last</a>
</li>
</ul>
@endif
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us