GroupMemberObserver.php 1.1 KB
<?php

namespace App\Observers;

use App\Models\GroupHasMember;
use App\Models\GroupInvite;
use App\Models\Groups;
use App\Models\UserMessage;


class GroupMemberObserver
{
    public function deleted(GroupHasMember $member): void
    {
        $groupId = $member->getAttribute('group_id');
        if (GroupHasMember::query()->where('group_id', $groupId)->where('role', '!=', 1)->doesntExist()) {
            $masterId = GroupHasMember::query()->where('group_id', $groupId)->where('role', 1)->value('member_id');
            UserMessage::query()
                ->where('sender_id', $member->getAttribute('member_id'))->orWhere('receiver_id', $masterId)
                ->where('type', 3)->where('is_bind', 1)->delete();
            //解散群把团长的邀请和申请全部置为无效
            GroupInvite::query()->where(['invite_id' => $masterId])->update(['status' => 4]);
            GroupInvite::query()->where(['user_id' => $masterId])->update(['status' => 4]);
            Groups::query()->whereKey($groupId)->delete();
            GroupHasMember::query()->where('group_id', $groupId)->delete();
        }
    }
}