n Laravel, observers are classes that listen for specific events in the application's lifecycle and perform actions in response to those events. Observers are commonly used in conjunction with Eloquent models to perform tasks such as sending notifications, logging changes, or updating related records when certain events occur. Here's how you can create an observer in Laravel:
Observers and events do not do the same thing at all.
An observer watches for specific things that happen within eloquent such as saving, saved, deleting, deleted (there are more but you should get the point). Observers are specifically bound to a model.
Events:
Events are actions that are driven by whatever the programmer wants. If you want to fire an event when somebody loads a page, you can do that. Unlike observers events can also be queue, and ran via laravels cron heartbeat. Events are programmer defined effectively. They give you the ability to handle actions that you would not want a user to wait for (excxample being the purchase of a pod cast)
The documentation does a very good job covering these.
Create the observer class:
This command generates a new observer class named
UserObserver
and associates it with theUser
model.Open the generated observer class (
app/Observers/UserObserver.php
) and define the event listeners and corresponding actions within its methods. For example:
s
<?php
namespace App\Observers;
use App\Models\User;
class UserObserver
{
/**
* Handle the User "created" event.
*
* @param \App\Models\User $user
* @return void
*/
public function created(User $user)
{
// Perform actions when a user is created
}
/**
* Handle the User "updated" event.
*
* @param \App\Models\User $user
* @return void
*/
public function updated(User $user)
{
// Perform actions when a user is updated
}
// Other event listener methods...
}
boot
method of your AppServiceProvider
or any other service provider:s
use App\Models\User;
use App\Observers\UserObserver;
// ...
public function boot()
{
User::observe(UserObserver::class);
}
Now, the UserObserver
will listen to events such as created
, updated
, deleted
, etc., for the User
model, and you can define the corresponding actions within the observer's event listener methods.
Regarding the difference between observers and events in Laravel:
Observers:
- Observers are specific to Eloquent models and allow you to listen to and respond to various events (e.g.,
created
,updated
,deleted
) for the associated model. - Observers are useful for encapsulating model-related logic and keeping it separate from the model itself.
- They enable you to perform actions before or after events occur on the model, without cluttering the model's code.
Events:
- Events, on the other hand, are a more general concept in Laravel that can be used throughout the application, not just with models.
- Events allow you to define custom events and trigger them at specific points in your application's code.
- With events, you can decouple components and make your code more modular by having listeners respond to events without being tightly bound to the code that triggers the events.
- Events can be used to trigger actions across different parts of your application, not limited to Eloquent models.
In summary, observers are specific to Eloquent models and are used to observe and respond to model-specific events, while events are a more general mechanism for decoupled communication between different parts of your Laravel application.
No comments:
Post a Comment