dimanche 7 juillet 2019

Eloquent: Skipping attachment if already attached

I have the following situation. There's a Controller that attaches those to another model with different pivot link_type.

    if ( $administrators = $request->get('administrators') ) {
        if (!empty($administrators)) {
            if (is_array($administrators[0])) {
                $administrators = array_map(function($item) {
                    return $item['id'];
                }, $administrators);
            }
            $administrators = User::whereIn('id', $administrators)->get();
            $message->users()->attach($administrators, ['link_type'=>'administrator']);
            $result['temp'][] = "Administrators;count:".$administrators->count();
        }
    }

    if ( $regional_managers = $request->get('regional_managers') ) {
        if (!empty($regional_managers)) {
            if (is_array($regional_managers[0])) {
                $regional_managers = array_map(function($item) {
                    return $item['id'];
                }, $regional_managers);
            }
            $regional_managers = User::byRole([User::ROLE_REGIONAL_MANAGER])->notSelf()->whereIn('id', $regional_managers)->get();
            $message->users()->attach($regional_managers, ['link_type'=>'regional_manager']);
            $result['temp'][] = "GroupManagers;count:".$regional_managers->count();
        }
    }

And there's a couple of more blocks like that.

Some users are being attached to the list as Regional Managers, Some are attached as Administrators. But there are duplicates at those.

Is there any way to skip attaching certain people with link type "regional_manager" if they have already been attached in the previous block with link_type "administrator"?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire