Laravel Pagination

Pagination with Laravel (5.4 at the time of writing) is very easy.

Say you want a table of users

In your controller:

$user = User::all()->paginate(10);

In the view loop through your users as normal and then add

$user->links() where ever you feel like it and more than one place if you are feeling dandy.

There is more..

But say you want more than one paginate-able list/table on the same page? This is the paginators signature

paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)

Easy, extending the original query lets get all the threads/comments for a user. Assuming your User model can dish out Threads and Comments

$user = User::find(1);
$threads = $user->threads()->paginate(4, ['*'], 'threads');
$comments = $user->comments()->paginate(4, ['*'], 'comments');

Then in your view loop through your comments and add

$comments->links();

Loop through your threads and add

$threads->links();

 

 

 

Laravel Real-Time Facades (Bastards!)

Come on Taylor Ortwell whats the deal? I spend time learning about Facades and their round-a-bout method of getting them registered and running. Now with Laravel 5.4 I can get the same outcome by typing a single line of code.

WTF?

What is a  Laravel Facade?

A Facade is a “static” interface for classes that are available in the application’s service container. Think functions on steroids!

Creating them WAS simple but there where a few loops you had to jump through. But now just declare them using the Facades namespace

use Facades/{location}/{of}/DoSomething;

and use them with

DoSomething::amazing()

No need to register your Facades. Just like before DI is taken care of.

Laravel Lovelyness

In case you don’t get it I think this new method of declaring Facades is a good thing.

Laravel Validation of unique fields

Laravel Validation of unique fields e.g email

Basic server side validation in Laravel is ridiculously easy. The following inside a controller will validate the passed values and redirect back if it fails.

$this->validate($request, [

            'firstName' => 'required',

            'lastName' => 'required',

            'email' => 'required|unique:users',

            'password' => 'required|confirmed',

            ]);

You can see what fields are required; the email has to be unique in the database users table and the password is required and must match a field named ‘password_comfirm’. When this validation passes you can then add the new user.

What about UPDATING or (PUT/PATCH) the user form is returned, the firstName has changed and the email remains the same. The original validation code will not pass and return an error of ‘email already in use’.

Solution

$this->validate($request, [

            'firstName' => 'required',

            'lastName' => 'required',

            'email' => 'required|unique:users,email,'.$id.'',

            ]);

The email’s unique rule now looks up the users table, looks for the email address except where the id column matches $id

For more information: https://laravel.com/docs/5.4/validation#rule-unique