This video will focus on building a token-based authentication system in Laravel 5.5. This is the default api authentication driver that Laravel uses. We will also set up an api_token field in our database and explore how we can set it to auto-populate using a little-known Laravel features called observers.

We start off by creating clearing our database and adjusting our original users table migration to add the api_token column. The reason we don’t just add this with a new migration is because we will be making the column a unique index and we already have records in our database which don’t contain unique api tokens. This causes exceptions to be thrown and because the data is all auto-generated anyway, we might as well just make life easy by removing all the data, running it fresh and re-seeding our database.

Before we seed, make sure to add the api token field in the LaraTrustSeeder.php file, so that all of our laratrust classes have api tokens to work with, since it is now required.

Now to add api_tokens to all of our other users, we will tie into the creating event, using observers.

Observers are a paradigm that allow them to watch our model for certain changes, and then jump in and manipulate or run functions with or against our model, before letting the process continue. To create an observer we will create a folder in our app folder for all future observers we might have, create the UserObserver.php file, and populate it with a basic observer template provided in the Laravel documentation.

After that, to get the observer working, we need to create a service provider that links our model to our observer and then make sure that the service provider is in our config/app file.

Once that is complete, we are ready to go. Any time we create a new user model now, the observer will jump in before the save and automatically create an api token and attach it to the model, before it passes it back to the model to complete the save.

Read also >>  Experienced PHP Developer - Wilmington, NC

This way we don’t actually have to worry about manually dealing with api_tokens, they will just get built for us automatically.

Hopefully this provided some new content that you can learn from. If you are new to the channel, be sure to subscribe, and remember you can always talk to me on twitter, or email me if necessary (all details below).


Use this snippet to generate the random api token:


=== LINKS ===

Laravel 5.5 Observer Documentation

Insomnia Rest Client (Mac, Windows, and Linux)

Postman Rest Client (Mac, Windows, and Linux)

Paw Rest Client (Mac Only)


Full Playlist for the “Building an Advanced Blog/CMS from Start to Finish” Series:


Download Code from this Episode:

==== FOLLOW ME ====

Subscribe for New Releases!

Subscribe to DevMarketer Insider

Twitter –
(ask me questions!)

==== QUESTIONS? ====

Leave a comment below and I or someone else can help you.
For quick questions you may also want to ask me on Twitter, I respond almost immediately.

Email me

Thanks for all your support!



Please enter your comment!
Please enter your name here