-4

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

8
  • 1
    Welcome to StackOverflow. Please review the guide on How to Ask. Commented Mar 17 at 12:17
  • 2
    Please edit your question and include your code as properly formatted text. Images are not helpful for code debugging, as we cannot copy and paste (or otherwise manipulate) your code if it's an image... Commented Mar 17 at 13:14
  • I would like a standard function! The images I posted are more to show the function names! I didn't code anything more than the model class and a line where to put the future function. Commented Mar 17 at 16:01
  • what is a "routine function"? "I've created it in laravel 10 with alpine tailwind,livewire and flowbite" - you have created all of that in your screenshot but don't know how to add a function? Commented Mar 17 at 16:08
  • in my function, there is $object->libraries()-><!< What am I writing here? > to search and delete duplicate items in my database by object Commented Mar 17 at 17:11

1 Answer 1

1

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());
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.