File: D:/HostingSpaces/SBogers10/ehboledensysteem.komma.pro/app/KommaApp/Users/UserEventService.php
<?php
namespace App\KommaApp\Users;
use App\KommaApp\Events\Models\Event;
use App\KommaApp\Users\Models\User;
class UserEventService
{
/**
* @param $user
* @param $event
* @param $subscribe
*/
public function subscribeUser(User $user, Event $event, bool $subscribed)
{
if(!$subscribed) {
$existingEvent = $user->events()->withPivot(['user_status'])->where('event_id', '=', $event->id)->first();
if($existingEvent) {
$user->events()->updateExistingPivot($existingEvent->id, ['user_status' => Event::UserStatusSubscribed]);
} else {
$user->events()->save($event, ['user_status' => Event::UserStatusSubscribed]);
}
} else { //Unsubscribe
$existingEvent = $user->events()->where('event_id', '=', $event->id)->first();
if($existingEvent) {
$user->events()->detach($existingEvent->id);
}
}
}
}