---
# System prepended metadata

title: Laravel 關聯事件和監聽器
tags: [Laravel]

---

# Laravel 關聯事件和監聽器

在 `EventServiceProvider` 中將事件和監聽器關聯起來
*app/Providers/EventServiceProvider.php*
```php=
use App\Events\OrderPaid;
use App\Listeners\UpdateProductSoldCount;
.
.
.
    protected $listen = [
        .
        .
        .
        OrderPaid::class => [ // 事件
            UpdateProductSoldCount::class, // 監聽器
        ],
    ];
```

### Update
Observer
*app/Providers/EventServiceProvider.php*
```php=
use App\Models\User;
use App\Observers\UserObserver;
.
.
.
    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        User::observe(UserObserver::class);
    }
```
[參考](https://laravel.com/docs/8.x/eloquent#defining-observers)

###### tags: `Laravel`