Laravel

Events in Laravel 5.1

Events in Laravel 5.1

An event is an action which is handled by the program.
Usually we will use an event to perform some action during some operation.
Here we can see few examples how we will use an events.

1. User tries to attempt a Login.
2. After Login.
3. After Logout.
4. During comment send notification.
5. During registration send Email.
6. While inserting update another table.
etc……

How to set up Events

Step 1 : Create Events Class

Run the below comment in terminal

It will create the Event file inside /App/Events/LogginSuccess.php

The file will look like below :

Step 2 : Modify the File like below



Step 3 : Create Event Listener file by Running the below comment.

It will create the Listener Class For an Event.

It will create the Listener file inside /App/Listeners/UpdateUserLastLogin.php

The file will look like below :

Step 4 : Modify Listner File Like below :

 Step 5 : Register your events in EventServiceProvider.php file.

 Step 6 : Fire your Event After user authentication Like below

Now the last_login and last_login_ip field updated for the logged in user.

That’s it. Now you can Fire the event like this for any operations.

Happy Coding!!.

If you like this article don’t forget to share and comment.
Thanks!.