jeudi 1 juin 2017

Update attach users Laravel 5.4 duplicate user already attched

I need to update attached user, i have user attach to project, i need to edit project by adding new attach user or delete attached user so my code look like this

$project = Project::find($id);
            $project->update($request->all());

           $array = $project->users;
           foreach ($array as $item){
               if($request->input('user_id') != $item->id){
                   Log::info('user new ids: ',[$item->id]);
                   $project->users()->attach($request->input('user_id'));
               }
               else{
                   Log::info('user aleardy attached');
               }
           }

        return response()->json($organisation);

This code duplicate users in database i find the same user assign to project several times, so how to resolve this issue and thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire