Sending emails, notifications and so on are jobs! You can queue them and do them later on.
So for example I want to register a user. There are a few things I want to do to get this going. First the user needs to fill in their credentials and I need to save that. That is one action. The second action would be sending an email to the user to tell them they need to confirm their registration.
Now you have two actions where one action can be queued and where one action ideally would be done at run time. So sending the email would be a job which can be called by an event of course.
UsersController extends Controller {
public function store(UserRequest $request)
{
$user = $this->userRepository->store($request); // User is already stored here
event(new UserWasRegistered($user); // Create some events
redirect()->withSuccess('Awesome you are registered now! Confirm that email');
}
}
Now we have that event we can just create some classes that will listen to that event
// app\Providers\EventServiceProvider.php
UserWasRegistered::class => [
SendRegisteredEmail::class
UpdateMemberCount::class
],
Now as you can see on the event the user was registered we call the SendRegisteredEmail class that will send the email to the user. We can add many actions here. Note that not all events needs to go to a queue. So for example UpdateMemberCount is clearing the cache of a query that get's the count of all the members. Now this is more a job then an event and it doesn't need to be queued at all. But it is still called by the event. Another example for deleting a user
/ app\Providers\EventServiceProvider.php
UserWasDeleted::class => [
SendDeletedEmail::class
UpdateMemberCount::class
],
As you can see we update the member count agian, well you get the idea
Note: I think I wondered off a bit...