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 ,
but when im trying to assign different projects and users it's duplicating the user1.. the user2 not attaching on other project
same as here when im assigning a single project to two or multiple users its not attaching only user 1, user 2 not working
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!');
}