phplaravelpolymorphismlaravel-10

Search duplicate items with the function morphToMany in Laravel


I want to create a routine function in my project. I've created it in Laravel 10 with Alpine, Tailwind, Livewire and Flowbite.

enter image description here

enter image description here

enter image description here

Which Laravel object do I use for this?

Thank you for the reply and have a nice day. Viviane Le Hegaret

None,I don't know to do that


Solution

  • 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());
                }
            }
        }