最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Search duplicate items with the function morphToMany in Laravel - Stack Overflow

programmeradmin3浏览0评论

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

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

Share Improve this question edited Mar 18 at 13:08 Archil Labadze 4,3254 gold badges28 silver badges45 bronze badges asked Mar 17 at 12:13 Viviane Le HegaretViviane Le Hegaret 113 bronze badges 8
  • 1 Welcome to StackOverflow. Please review the guide on How to Ask. – Vansh Patel 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... – Tim Lewis 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. – Viviane Le Hegaret 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? – kris gjika 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 – Viviane Le Hegaret Commented Mar 17 at 17:11
 |  Show 3 more comments

1 Answer 1

Reset to default 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());
            }
        }
    }
发布评论

评论列表(0)

  1. 暂无评论