Lesson 4. Index, List Table and Pagination

Founder of QuickAdminPanel
Mini-course: How To Create Admin Panel in Laravel 5.4
- Lesson 1. Laravel and Default Auth
- Lesson 2. CRUDs, Migrations and Models
- Lesson 3. Menu and Routes
- Lesson 4. Index, List Table and Pagination
- Lesson 5. Create, Update Entries and Validation
- Lesson 6. Deleting Entries and Some JavaScript
- Lesson 7. Roles, Permissions and Authorization
- Lesson 8. Do It All Much Quicker with QuickAdminPanel
In this chapter we will take care about the list table of authors. It’s pretty simple, actually.
Basic Controller and View
Here’s how the code looks in /app/Http/Controllers/AuthorsController.php:
public function index() { $authors = Author::all(); return view('authors.index', compact('authors')); }
Basically, we’re taking all the items and passing them to the view as parameter.
Now, let’s create resources/views/authors/index.blade.php, using the same layouts/app generated before and Bootstrap classes for the table:
@extends('layouts.app') @section('content') <div class="container"> <div class="row"> <div class="col-md-8 col-md-offset-2"> <div class="panel panel-default"> <div class="panel-heading">Authors</div> <div class="panel-body"> <table class="table table-bordered"> <thead> <tr> <th>First name</th> <th>Last name</th> <th>Actions</th> </tr> </thead> <tbody> @forelse($authors as $author) <tr> <td>{{ $author->first_name }}</td> <td>{{ $author->last_name }}</td> <td> <a href="{{ route('authors.edit', $author->id) }}" class="btn btn-default">Edit</a> <form action="{{ route('authors.destroy', $author->id) }}" method="POST" style="display: inline" onsubmit="return confirm('Are you sure?');"> <input type="hidden" name="_method" value="DELETE"> {{ csrf_field() }} <button class="btn btn-danger">Delete</button> </form> </td> </tr> @empty <tr> <td colspan="3">No entries found.</td> </tr> @endforelse </tbody> </table> </div> </div> </div> </div> </div> @endsection
Here’s the visual result of it all (I’ve entered a test entry directly to the database).
There are a few things to explain here.
1. Forelse: probably some of you won’t know this structure – it’s just a more convenient way to write foreach without checking if the array is empty. So instead of having if-else and foreach – there’s a Blade forelse operator.
2. Form for DELETE method: that’s a usual Laravel way of deleting entries – you make a POST request with parameter _method=DELETE and entry ID and then it automatically matches destroy() method of the Controller (we will talk about it later).
Everything else should be pretty clear.
Pagination
What if we have more entries and need to have pagination? Laravel has pretty awesome functionality in core, without any external packages. Let’s see how it works.
First, we use paginate() method in Controller instead of all():
public function index() { $authors = Author::paginate(5); return view('authors.index', compact('authors')); }
Next – to display the pagination in our list in the file resources/views/authors/index.blade.php we just use method links() like this:
<table> ... </table> {{ $authors->links() }}
In case of 1-5 entries, it won’t display anything, but beyond that we will see something like this:
Guess what – if we click “Page 2” link or the arrow to the right, it will actually work!
As you can see, paginator will add a GET parameter to the URL ?page=2, and we don’t need to add any custom code to make it work. Isn’t that awesome?
Of course, you can perform a lot of customizations with this pagination – move the page length to config, change URL parameters and add your own ones, apply different styling than default etc. But I will leave it for you to play around, if you wish. More information about pagination in Laravel can be found in the official documentation.
Additional things
Now, we’ve covered just a basic list of entries. For bigger projects and bigger amount of data there are needs for more functionality, like:
- Filtering and searching through the data
- Sorting by columns
- Add additional fields with relationships
- Limit some buttons or even columns by logged in user
…and much more. I won’t show it in this course, but I do recommend to try out an awesome JavaScript library called Datatables. If you apply it to your data list, it will have automatic search, filter, sort and other functions.
I guess that’s it about the list of entries, now let’s go to the next lesson, where we will add create and update functionality.
- Lesson 1. Laravel and Default Auth
- Lesson 2. CRUDs, Migrations and Models
- Lesson 3. Menu and Routes
- Lesson 4. Index, List Table and Pagination
- Lesson 5. Create, Update Entries and Validation
- Lesson 6. Deleting Entries and Some JavaScript
- Lesson 7. Roles, Permissions and Authorization
- Lesson 8. Do It All Much Quicker with QuickAdminPanel
Try our new 2019.quickadminpanel.com!
13 Comments
Leave a Reply Cancel reply
Recent Posts
- Laravel: How to Add New Registered User to Mailchimp List
- One-To-Many with Soft-Deletes. Deleting Parent: Restrict or Cascade?
- AJAX Datatables: Move View/Edit/Delete Column from Right to Left Side
- Laravel + FullCalendar: Create/Edit Recurring Events
- Why it’s important to change APP_URL in Laravel .env file
Try our QuickAdminPanel Generator!
How it works:
1. Generate panel online
No coding required, you just choose menu items.
2. Download code & install locally
Install with simple "composer install" and "php artisan migrate".
3. Customize anything!
We give all the code, so you can change anything after download.
First off all nice tut, something I was looking for.
But when following the first step of lesson 4 I get error sayin this.
Class ‘App\Http\Controllers\AuthorsController\Controller’ not found.
I tried searching for online solution but it didnt work. Also I started all over again and copy past the whole thing and still nothing. I’m looking for good advice or a solution.
Sorry to bother you guys,
Cheers,
Hi, add a line on top: use App\Http\Controllers\Controller;
Tried that, it still isn’t working.
Ok google for “laravel controller not found”.
Related links:
https://stackoverflow.com/questions/33711379/class-controller-not-found-in-laravel-5
https://laracasts.com/discuss/channels/general-discussion/class-apphttpcontrollerscontroller-not-found
In your AuthorsController add the line below.
use App\Author;
use App\Author worked as well
I have the same issue as mentioned above and I have tried all the solutions mentioned above or in Google and nothing works for me 🙁
Kindly please help to resolve the issue.
Add this to your AuthorController,php . This will resolve your issue.
use App\Author;
I Hope someone can help me with this,
Exception message: syntax error, unexpected ‘number’ (T_STRING), expecting ‘,’ or ‘)’
This is what inside my code:
@forelse($coach as $coach)
{{ $coach->id number }}
{{ $coach->first name }}
{{ $coach->last name }}
{{ $coach->middle name }}
{{ $coach->email }}
{{ $coach->password }}
{{ $coach->contact }}
{{ $coach->team }}
{{ $coach->section }}
id) }}” class=”btn btn-default”>Edit
id) }}” method=”POST”
style=”display: inline”
onsubmit=”return confirm(‘Are you sure?’);”>
{{ csrf_field() }}
Delete
Ok this error if fine now
Please help me with this error…
“Missing required parameters for [Route: coach.destroy] [URI: admin/coach/{coach}]. (View: C:\Users\Erik\Admin\resources\views\coach\index.blade.php)”
This is the error:
FatalThrowableError
Parse error: syntax error, unexpected ‘number’ (T_STRING), expecting ‘,’ or ‘)’
in edcae0258f9096b202ce8c0a94fc758a0f41260e.php line 27
This is what inside in line 27:
{{ $coach -> id number }}
Please help me, I need to run this application for academic purpose.
I’m really new to laravel…
This is actually good, but i have a problem displaying my page.
it does not display anything, just a white blank page, and ther ‘is no error.
Anyone who can help me?
I’m using laravel 5.4 in windows os