Set up an observer to update doublers whenever an entry is created, modified, or deleted.
This commit is contained in:
parent
f3013670a3
commit
1f635e6ecf
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Events\AuditionChange;
|
||||
use App\Events\EntryChange;
|
||||
use App\Models\Audition;
|
||||
use App\Models\Doubler;
|
||||
use App\Models\Entry;
|
||||
|
||||
class EntryObserver
|
||||
|
|
@ -13,6 +13,16 @@ class EntryObserver
|
|||
*/
|
||||
public function created(Entry $entry): void
|
||||
{
|
||||
// Count how many entries the student has for the event
|
||||
$count = $entry->student->entriesForEvent($entry->audition->event_id)->count();
|
||||
|
||||
// If less than two entries, they're not a doubler
|
||||
if ($count < 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Update doublers for the event
|
||||
Doubler::syncForEvent($entry->audition->event_id);
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -21,7 +31,12 @@ class EntryObserver
|
|||
*/
|
||||
public function updated(Entry $entry): void
|
||||
{
|
||||
|
||||
// Update doubler table when an entry is updated
|
||||
Doubler::syncForEvent($entry->audition->event_id);
|
||||
if ($entry->wasChanged('audition_id')) {
|
||||
$originalData = $entry->getOriginal();
|
||||
Doubler::syncForEvent($originalData->audition->event_id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -29,7 +44,9 @@ class EntryObserver
|
|||
*/
|
||||
public function deleted(Entry $entry): void
|
||||
{
|
||||
|
||||
Doubler::where('student_id', $entry->student_id)->delete();
|
||||
$audition = Audition::where('id', $entry->audition_id)->first();
|
||||
Doubler::syncForEvent($audition->event_id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue