Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Laravel Pagination

Introduction to Laravel Pagination

The Laravel Pagination query allows the user to view all the display items on separate pages. There may hundreds of records that cannot be viewed at one go, so you put in page numbers as links which will then redirect you to the correct page, once clicked.

Examples to Implement Laravel Pagination

Let us start with a quick examples of Laravel Pagination:

Example #1

Code:

$customers = Customer::paginate(10);
@foreach ($customers as $customer)
@endforeach
First name  Last name  Email
{{ $customer->first_name }}    {{ $customer->last_name }}    {{ $customer->email }}
{{ $customers->links() }}

Output:

Code:

$customers = Customer::simplePaginate(10);

Output:

Explanation: The above example illustrates the range of the flexibility that the Laravel Framework provides to its developers. The pagination ideally provides page numbers while a simple paginate query asks the viewer to move to the next page, instead of providing him or her with a list of pages. The advantage of having a paginated query is that one can jump pages. So for example, if you had to go to page 6 directly from page 2, you can.

Example #2

Code:

composer create-project --prefer-distlaravel/laravel blog
php artisan migrate
php artisan tinker
factory(\App\User::class, 100)->create();
Route::get('pagination', 'PaginationController@index');
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class PaginationController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$users = User::paginate(5);
return view('users', compact('users'));
}
}

HTML Code:




Laravel 5.7 -  Pagination Link Customizations - ItSolutionStuff.com




Laravel 5.7 -  Pagination Link Customizations - ItSolutionStuff.com








@foreach($users as $user)





@endforeach
IDNameEmail
{{ $user->id }}{{ $user->name }}{{ $user->email }}

{{ $users->onEachSide(1)->links() }}


PHP Code:

php artisan serve
http://localhost:8000/pagination

Output:

Example #3

Code:

Route::get ( ‘/’, function () {
$dummyDetails = User::paginate(25);
return view ( ‘welcome’ )->withUsers($dummyDetails);
} );


@if(isset($users))

Sample User details










@foreach($users as $dummy)




@endforeach

NameEmail
{{$dummy->name}}{{$dummy->email}}

{!! $users->render() !!}@endif

Output:

Code:



{{ csrf_field() }}

placeholder="Search users">





Route::any ( '/search', function () {
$q = Input::get ( 'q' );
if($q != ""){
$user = User::where ( 'name', 'LIKE', '%' . $q . '%' )->orWhere ( 'email', 'LIKE', '%' . $q . '%' )->paginate (5)->setPath ( '' );
$pagination = $user->appends ( array (
'q' =>Input::get ( 'q' )
) );
if (count ( $user ) > 0)
return view ( 'welcome' )->withDetails ( $user )->withQuery ( $q );
}
return view ( 'welcome' )->withMessage ( 'No Details found. Try to search again !' );
} );

Output:

Explanation: The best part of the pagination query is that it can be added to any other laravel framework query. As the above example has shown, the database can be limited to just 5 output and the paginator can be added. This is also the reason why The Laravel Framework is such a sought after platform. The sheer options that it provides to the developer are praiseworthy.

Code:

public function index()
{
$authors = Author::all();
return view('authors.index', compact('authors'));
}
@forelse($authors as $author)
@empty
@endforelse
First name  Last name  Actions
{{ $author->first_name }}         {{ $author->last_name }}
Edit
{{ csrf_field() }}
Delete
No entries found.

Output:

Code:

public function index()
{
$authors = Author::paginate(5);
return view('authors.index', compact('authors'));
}

Output:

This is for page 1 and then when we go to Page 2:

Explanation: As I mentioned earlier, the Pagination query is able to perform in conjunction with the other queries, which help in providing the user with a better experience.

Example #4

A final example will suffice:

Code:

php artisan migrate
"require-dev": {
"filp/whoops": "~2.0",
"nunomaduro/collision": "~1.1",
"fzaninotto/faker": "~1.4",
"mockery/mockery": "~1.0",
"phpunit/phpunit": "~7.0",
"symfony/thanks": "^1.0"
},
// DatabaseSeeder.php
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$faker = Faker::create();
foreach (range(1,1000) as $index) {
DB::table('users')->insert([
'name' => $faker->name,
'email' => $faker->email,
'password' =>bcrypt('secret'),
]);
}
}
php artisan db:seed
// HomeController.php
use App\User;
public function getUsers()
{
$users = User::all();
return view('index', compact('users'));
}
// web.php
Route::get('/users', 'HomeController@getUsers')->name('users');





Users Data














@foreach($users as $user)





@endforeach

IDNameEmail
{{ $user->id }}{{ $user->name }}{{ $user->email }}




// HomeController.php
$users = User::paginate(15);
{{ $users->links() }}





Users Data














@foreach($users as $user)





@endforeach

IDNameEmail
{{ $user->id }}{{ $user->name }}{{ $user->email }}

{{ $users->links() }}


Output:

Conclusion

The Pagination query is to create a vibe of a book within the confines of the web page. More importantly, the navigation is simplified and the user now can reach any page and in any order.

Recommended Articles

This is a guide to Laravel Pagination. Here we discuss an introduction to Laravel Pagination and examples with code, output and explanation. You can also go through our other related articles to learn more –

  1. Laravel Clear Cache
  2. Laravel Form Builder
  3. Laravel Errors
  4. Laravel Deploy
  5. Laravel Permissions

The post Laravel Pagination appeared first on EDUCBA.



This post first appeared on Best Online Training & Video Courses | EduCBA, please read the originial post: here

Share the post

Laravel Pagination

×

Subscribe to Best Online Training & Video Courses | Educba

Get updates delivered right to your inbox!

Thank you for your subscription

×