Home > Software design >  Laravel - pivot attach multiple using nested loop. duplicating user_id
Laravel - pivot attach multiple using nested loop. duplicating user_id

Time:05-27

here is my problem the user_id is duplicating other user_id is not applying or detaching using nested loop, i think the problem is in my ProjectController i will show the other photos for specific to describe my problem.

when selecting two projects assigning to single user1 its working fine. its not duplicating ,

like in this phot:enter image description here

but when im trying to assign different projects and users it's duplicating the user1.. the user2 not attaching on other projectenter image description here

same as here when im assigning a single project to two or multiple users its not attaching only user 1, user 2 not workingenter image description here

this is my blade file. im using name[] and project[]

 <form method="POST"  action="{{ route('home.create') }}">
                    @csrf
                    @method('POST')
                    <div >
                    <div >
                        <label for="" >Project</label>
                        <select  name="project[]" >
                          @foreach ($projects as $project)
                          <option value="{{ $project->id }}">{{ $project->name }}</option>
                          @endforeach
                    </select>
                    </div>
                    <div >
                        <label for="" >Name</label>
                        <select name="user[]" >
                          @foreach ($users as $user)
                          <option value="{{ $user->id }}">{{ $user->name }}</option>
                          @endforeach
                    </select>
                    </div>
                    </div>
                    <div >
                        <div >
                            <select  name="project[]" >
                              @foreach ($projects as $project)
                              <option value="{{ $project->id }}">{{ $project->name }}</option>
                              @endforeach
                        </select>
                        </div>
                        <div >
                            <select name="user[]" >
                              @foreach ($users as $user)
                              <option value="{{ $user->id }}">{{ $user->name }}</option>
                              @endforeach
                        </select>
                        </div>
                        </div>
                 <button type="submit" >Submit</button>
   </form>

this is my Model Project

class Project extends Model
{
    use HasFactory;

    protected $fillable = ['name'];

    public function users(){
        return $this->belongsToMany(User::class);
    }
} 

this is my migration project_user

   public function up()
    {
        if (!Schema::hasTable('project_user')) {
        Schema::create('project_user', function (Blueprint $table) {
           $table->foreignId('project_id')->constrained()->onDelete();
           $table->foreignId('user_id')->constrained()->onDelete();
        });

        };
    }

this is my ProjectController i think my code is wrong for making nested attach to my pivot table that's why when im trying to assign other user_id to other project or single its duplicating

public function create(Request $request)
{
    foreach($request->input('project') as $project){
        $list=[];
        $projects = Project::find($project);
        foreach($request->input('user') as $user){
         $users = User::find($user);
         $list[$projects->id] = ['user_id'=>$users->id];
        }
     $projects->users()->attach($list);        
    return redirect()->back()->with('success','Successful created!');
}

please help to solve this problem, i tried many times but i can't still solve it

CodePudding user response:

The problem is in the create method. Through the form you send two arrays. To know which user you must add to the project you must consult the index of the arrays, so that you must assign the user of users[i] to the project of projects[i]

Another mistake is that the attach method must be used on a model object and you can pass it an id or an array of ids. However you are trying to attach to a project an associative array [project_id => user_id].

I have also renamed some variables to better fit what they represent

public function create(Request $request)
{
    foreach($request->input('project') as $index => $project_id){
        $project = Project::find($project_id);
        $project->users()->attach($request->input('user')[$index]);     
     }    

    return redirect()->back()->with('success','Successful created!');
}
  • Related