user(); assert($user !== null); // Only types that can email at all have anything to opt in or out // of — a pure in-app type has no toggle to show. Either route // counts: Notifier sending a mail class directly, or the digest // buffering and sending one. $emailable = array_values(array_filter( $this->types->all(), fn ($type) => $type->mailNotification !== null || $type->digestMail !== null, )); return Inertia::render('settings/notifications', [ 'types' => array_map(fn ($type) => [ 'key' => $type->key, 'label' => $type->label, 'email_enabled' => $this->preferences->emailEnabledFor($user, $type), ], $emailable), ]); } public function update(Request $request): RedirectResponse { $user = $request->user(); assert($user !== null); $validated = $request->validate([ 'preferences' => ['required', 'array'], 'preferences.*.type' => ['required', 'string'], 'preferences.*.email_enabled' => ['required', 'boolean'], ]); foreach ($validated['preferences'] as $preference) { NotificationPreference::query()->updateOrCreate( ['user_id' => $user->id, 'type' => $preference['type']], ['email_enabled' => $preference['email_enabled']], ); } return back(); } }