수색…
옵저버 생성하기
 옵저버는 Laravel의 특정 모델에 대한 라이브 사이클 콜백을 청취하는 데 사용됩니다. 
 이러한 리스너는 다음 작업 중 하나를들을 수 있습니다. 
- 창조
- 만들어진
- 업데이트 중
- 업데이트 된
- 절약
- 저장된
- 삭제 중
- 삭제 된
- 복원 중
- 복원 된
다음은 관찰자의 예입니다.
UserObserver
<?php
namespace App\Observers;
/**
 * Observes the Users model
 */
class UserObserver 
{
    /**
     * Function will be triggerd when a user is updated
     *
     * @param Users $model
     */
     public function updated($model)
     {
         // execute your own code
     }
}
 사용자 관찰자에 표시된 것처럼 업데이트 된 동작을 수신하지만이 클래스가 실제로 사용자 모델을 수신하기 전에 먼저 EventServiceProvider 에 등록해야합니다. 
EventServiceProvider
<?php 
namespace App\Providers;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use App\Models\Users;
use App\Observers\UserObserver;
/**
 * Event service provider class
 */
class EventServiceProvider extends ServiceProvider
{
    /**
     * Boot function
     *
     * @param DispatcherContract $events
     */
    public function boot(DispatcherContract $events)
    {
        parent::boot($events);
        // In this case we have a User model that we want to observe
        // We tell Laravel that the observer for the user model is the UserObserver
        Users::observe(new UserObserver());
    }
}
관찰자를 등록 했으므로 사용자 모델 저장 후 매번 업데이트 된 함수가 호출됩니다.
Modified text is an extract of the original Stack Overflow Documentation
        아래 라이선스 CC BY-SA 3.0
        와 제휴하지 않음 Stack Overflow