在 Laravel 5.1 中使用 sync() 将附加数据传递给中间 table
Passing additional data to the intermediate table using sync() in Laravel 5.1
我有以下内容,按预期工作:
$aircraft->logbook_entries()->sync($request['tasks']);
我想将“work_order_id”添加到我的数据透视表 table,因此我执行了以下操作:
- 在适当的
belongsToMany
关系中将 ->withPivot('work_order_id')
添加到 Aircraft
和 Task
模型
- 将我的查询更新为
$aircraft->logbook_entries()->sync([1 => ['work_order_id' => $workorder->id], $request['tasks']]);
这不会根据需要将 work_order_id
添加到枢轴 table。相反,我收到错误:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (pams_amo.airframe_logbook_entries, CONSTRAINT airframe_logbook_entries_work_order_id_foreign FOREIGN KEY (work_order_id) REFERENCES work_orders (id)) (SQL: insert into airframe_logbook_entries (aircraft_id, created_at, task_id, updated_at) values (1, 2019-03-04 22:11:48, 12, 2019-03-04 22:11:48))
我遵循了 Laravel 5.1 文档:Many To Many Relationships - syncing for convenience 上面写着:
Syncing For Convenience
You may also use the sync method to construct many-to-many associations. The sync method accepts an array of IDs to place on the intermediate table. Any IDs that are not in the given array will be removed from the intermediate table. So, after this operation is complete, only the IDs in the array will exist in the intermediate table:
$user->roles()->sync([1, 2, 3]);
You may also pass additional intermediate table values with the IDs:
$user->roles()->sync([1 => ['expires' => true], 2, 3]);
如果您想传递一个额外的列,您误解了同步方法的工作原理。您所要做的就是将同步方法调用为:
$tasks = array_combine(
$request['tasks'],
array_fill(0, count($request['tasks']), ['work_order_id' => $workorder->id])
);
$aircraft->logbook_entries()->sync($tasks);
所以,这个想法是,如果你想将一个附加参数传递给数据透视表,任务 ID 将成为数组键,附加参数将作为数组值发送
我有以下内容,按预期工作:
$aircraft->logbook_entries()->sync($request['tasks']);
我想将“work_order_id”添加到我的数据透视表 table,因此我执行了以下操作:
- 在适当的
belongsToMany
关系中将->withPivot('work_order_id')
添加到Aircraft
和Task
模型 - 将我的查询更新为
$aircraft->logbook_entries()->sync([1 => ['work_order_id' => $workorder->id], $request['tasks']]);
这不会根据需要将 work_order_id
添加到枢轴 table。相反,我收到错误:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (pams_amo.airframe_logbook_entries, CONSTRAINT airframe_logbook_entries_work_order_id_foreign FOREIGN KEY (work_order_id) REFERENCES work_orders (id)) (SQL: insert into airframe_logbook_entries (aircraft_id, created_at, task_id, updated_at) values (1, 2019-03-04 22:11:48, 12, 2019-03-04 22:11:48))
我遵循了 Laravel 5.1 文档:Many To Many Relationships - syncing for convenience 上面写着:
Syncing For Convenience
You may also use the sync method to construct many-to-many associations. The sync method accepts an array of IDs to place on the intermediate table. Any IDs that are not in the given array will be removed from the intermediate table. So, after this operation is complete, only the IDs in the array will exist in the intermediate table:
$user->roles()->sync([1, 2, 3]);
You may also pass additional intermediate table values with the IDs:
$user->roles()->sync([1 => ['expires' => true], 2, 3]);
如果您想传递一个额外的列,您误解了同步方法的工作原理。您所要做的就是将同步方法调用为:
$tasks = array_combine(
$request['tasks'],
array_fill(0, count($request['tasks']), ['work_order_id' => $workorder->id])
);
$aircraft->logbook_entries()->sync($tasks);
所以,这个想法是,如果你想将一个附加参数传递给数据透视表,任务 ID 将成为数组键,附加参数将作为数组值发送