Laravel - 使用嵌套循环枢轴附加多个。复制 user_id
Laravel - pivot attach multiple using nested loop. duplicating user_id
这是我的问题 user_id 正在复制其他 user_id 没有使用嵌套循环应用或分离,我认为问题出在我的 ProjectController我会展示其他照片来具体描述我的问题。
选择分配给单个用户的两个项目时,其工作正常。它没有重复,
喜欢这张照片:
但是当我尝试分配不同的项目和用户时,它复制了用户 1.. 用户 2 没有附加到其他项目
与此处相同,当我将单个项目分配给两个或多个用户时,它不只附加用户 1,用户 2 不工作
这是我的 blade 文件。我正在使用 name[] 和 project[]
<form method="POST" class="mx-8 space-x-4" action="{{ route('home.create') }}">
@csrf
@method('POST')
<div class="flex space-x-4">
<div class="inline-block relative w-64 mb-2">
<label for="" class="mb-5 text-lg">Project</label>
<select name="project[]" class="p-2 block appearance-none bg-white border border-gray-400 hover:border-gray-500 px-4 py-2 pr-8 rounded shadow leading-tight focus:outline-none focus:shadow-outline">
@foreach ($projects as $project)
<option value="{{ $project->id }}">{{ $project->name }}</option>
@endforeach
</select>
</div>
<div class="inline-block relative w-64 mb-2">
<label for="" class="mb-5 text-lg">Name</label>
<select name="user[]" class="p-2 block appearance-none bg-white border border-gray-400 hover:border-gray-500 px-4 py-2 pr-8 rounded shadow leading-tight focus:outline-none focus:shadow-outline">
@foreach ($users as $user)
<option value="{{ $user->id }}">{{ $user->name }}</option>
@endforeach
</select>
</div>
</div>
<div class="flex space-x-4">
<div class="inline-block relative w-64 mb-2">
<select name="project[]" class="p-2 block appearance-none bg-white border border-gray-400 hover:border-gray-500 px-4 py-2 pr-8 rounded shadow leading-tight focus:outline-none focus:shadow-outline">
@foreach ($projects as $project)
<option value="{{ $project->id }}">{{ $project->name }}</option>
@endforeach
</select>
</div>
<div class="inline-block relative w-64 mb-2">
<select name="user[]" class="p-2 block appearance-none bg-white border border-gray-400 hover:border-gray-500 px-4 py-2 pr-8 rounded shadow leading-tight focus:outline-none focus:shadow-outline">
@foreach ($users as $user)
<option value="{{ $user->id }}">{{ $user->name }}</option>
@endforeach
</select>
</div>
</div>
<button type="submit" class="bg-green-300 border border-gray-300 p-3">Submit</button>
</form>
这是我的模型项目
class Project extends Model
{
use HasFactory;
protected $fillable = ['name'];
public function users(){
return $this->belongsToMany(User::class);
}
}
这是我的迁移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();
});
};
}
这是我的 ProjectController 我认为我的代码将嵌套附加到我的枢轴是错误的 table 这就是为什么我试图分配其他 user_id到其他项目或单独复制
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!');
}
请帮忙解决这个问题,我试了很多次了还是无法解决
问题出在创建方法上。通过您发送两个数组的形式。要知道您必须将哪个用户添加到项目中,您必须查阅数组的索引,以便您必须将 users[i] 的用户分配给 projects[i]
的项目
另一个错误是attach方法必须用在模型对象上,你可以给它传递一个id或一个id数组。但是,您正在尝试将关联数组 [project_id => user_id].
附加到项目
我还重命名了一些变量以更好地适应它们所代表的内容
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!');
}
这是我的问题 user_id 正在复制其他 user_id 没有使用嵌套循环应用或分离,我认为问题出在我的 ProjectController我会展示其他照片来具体描述我的问题。
选择分配给单个用户的两个项目时,其工作正常。它没有重复,
喜欢这张照片:
但是当我尝试分配不同的项目和用户时,它复制了用户 1.. 用户 2 没有附加到其他项目
与此处相同,当我将单个项目分配给两个或多个用户时,它不只附加用户 1,用户 2 不工作
这是我的 blade 文件。我正在使用 name[] 和 project[]
<form method="POST" class="mx-8 space-x-4" action="{{ route('home.create') }}">
@csrf
@method('POST')
<div class="flex space-x-4">
<div class="inline-block relative w-64 mb-2">
<label for="" class="mb-5 text-lg">Project</label>
<select name="project[]" class="p-2 block appearance-none bg-white border border-gray-400 hover:border-gray-500 px-4 py-2 pr-8 rounded shadow leading-tight focus:outline-none focus:shadow-outline">
@foreach ($projects as $project)
<option value="{{ $project->id }}">{{ $project->name }}</option>
@endforeach
</select>
</div>
<div class="inline-block relative w-64 mb-2">
<label for="" class="mb-5 text-lg">Name</label>
<select name="user[]" class="p-2 block appearance-none bg-white border border-gray-400 hover:border-gray-500 px-4 py-2 pr-8 rounded shadow leading-tight focus:outline-none focus:shadow-outline">
@foreach ($users as $user)
<option value="{{ $user->id }}">{{ $user->name }}</option>
@endforeach
</select>
</div>
</div>
<div class="flex space-x-4">
<div class="inline-block relative w-64 mb-2">
<select name="project[]" class="p-2 block appearance-none bg-white border border-gray-400 hover:border-gray-500 px-4 py-2 pr-8 rounded shadow leading-tight focus:outline-none focus:shadow-outline">
@foreach ($projects as $project)
<option value="{{ $project->id }}">{{ $project->name }}</option>
@endforeach
</select>
</div>
<div class="inline-block relative w-64 mb-2">
<select name="user[]" class="p-2 block appearance-none bg-white border border-gray-400 hover:border-gray-500 px-4 py-2 pr-8 rounded shadow leading-tight focus:outline-none focus:shadow-outline">
@foreach ($users as $user)
<option value="{{ $user->id }}">{{ $user->name }}</option>
@endforeach
</select>
</div>
</div>
<button type="submit" class="bg-green-300 border border-gray-300 p-3">Submit</button>
</form>
这是我的模型项目
class Project extends Model
{
use HasFactory;
protected $fillable = ['name'];
public function users(){
return $this->belongsToMany(User::class);
}
}
这是我的迁移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();
});
};
}
这是我的 ProjectController 我认为我的代码将嵌套附加到我的枢轴是错误的 table 这就是为什么我试图分配其他 user_id到其他项目或单独复制
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!');
}
请帮忙解决这个问题,我试了很多次了还是无法解决
问题出在创建方法上。通过您发送两个数组的形式。要知道您必须将哪个用户添加到项目中,您必须查阅数组的索引,以便您必须将 users[i] 的用户分配给 projects[i]
的项目另一个错误是attach方法必须用在模型对象上,你可以给它传递一个id或一个id数组。但是,您正在尝试将关联数组 [project_id => user_id].
附加到项目我还重命名了一些变量以更好地适应它们所代表的内容
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!');
}