79520466

Date: 2025-03-19 14:24:45
Score: 0.5
Natty:
Report link

I found it! You had to add a condition to tell it that if it finds 2 or more identical objects, you have to delete the relationship and recreate one.

I'm closing the discussion. Have a nice day.

public function updateUser($object){
        $userToChange=$object->users()->first();
        if (empty($userToChange)) {
            $object->users()->save(User::where('id',Auth::user()->getAuthIdentifier())->first());
        }elseif($object->users()->where('user_id', $userToChange->id)->count() >= 1){ //add condition to detect if several identical objects exists
            $object->users()->detach($userToChange->id);
            $object->users()->save(User::where('id',Auth::user()->getAuthIdentifier())->first());
        }else {
            if(Auth::user()->getAuthIdentifier()!=$userToChange->id){
                $object->users()->sync(User::where('id',Auth::user()->getAuthIdentifier())->first());
            }
        }
    }
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Viviane Le Hegaret