I have a model called Lessons and it has a belongsToMany relationship called students with a table called students_for_lesson. Lesson model has fields called number_of_students and number_of_enrollments for each lesson.
what I want is to give a message stopping Add students for the lesson when number_of_enrollments value reaches number_of_students value.
One way is to listen to the Model relation events (
BelongsToMany) : beforeAttach, afterAttach, beforeDetach, afterDetachIn this case if you need to run some validations before creating the relationship then use the
beforeAttachevent:See this SO post & here about extending models