2017-02-09 2 views
1

을 사용하여 여러/무한 번 트리거되고 난 다음 코드가 있습니다피 웅변 모델 이벤트는 Laravel

AppServiceProvider :

User 모델에서
User::saved(function (User $user) { 
    return $user->modifiedEvent(); 
}); 

User::deleted(function (User $user) 
    return $user->modifiedEvent(); 
}); 

내가 가진 :

public function modifiedEvent() 
    { 
     $stage = $this->isDirty('terms_and_conditions') 
      && $this->terms_and_conditions !== null ? 'completed' : 'incomplete'; 
     $this->stage_terms_and_conditions = $stage; 
     return $this->save(); 
    } 

을 문제는 modifiedEvent도 모델을 수정한다는 것인데, 이는 무한 루프가 발생한다는 것을 의미합니다!

이 이벤트가 자체 발생 이벤트로 인해 여러 번 발생하지 않도록하는 가장 좋은 방법은 무엇입니까?

답변

2

'저장' 이벤트를 사용하려고하므로 끝에 $ this-> save()를 호출 할 필요가 없습니다.