HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
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);
            }
        }
    }
}