Laravel Livewire 同步数据透视表
我有一个 select2 多个下拉菜单,允许用户在创建用户时选择多个位置来附加新用户。
我在验证数据后使用 sync()
来存储位置数组。我收到以下错误:
SQLSTATE[23000]:违反完整性约束:1452 无法添加或 更新子行:外键约束失败 (
location_user
,约束location_user_location_id_foreign
外键 (location_id
) REFERENCESlocations
(id
)) (SQL:插入location_user
(id
、location_id
、user_id
) 值 (1001, 0, 3))
我理解这个错误,因为我有数据库背景,什么我不明白如何让 Laravel 插入正确的数据。插入语句当前为:
(id, location_id, user_id) 值 (1001, 0, 3)
但这些值位于错误的位置或映射不正确。 1001
是位置 ID,但映射到 id
,大概是因为我的数组中有 id
,它也拉入 0 大概是因为它是数组键,但这不是任何东西的实际值。
我希望它忽略数据透视表上的 id 字段,只插入 (location_id, user_id) 值 (1001, 3)
这是我的数组在验证后的样子:
这是我的 Livewire 组件中的代码:
class Create extends Component
{
public $user;
public $first_name;
public $last_name;
public $email;
public $phone;
public $role_id;
public $roles;
public $locations;
protected $rules = [
'first_name' => ['required', 'string', 'max:150'],
...
'role_id' => 'required|exists:roles,id',
'locations.*.id' => 'required|exists:locations,id'
];
public function mount()
{
$this->locations = Auth::user()->locations;
}
public function render()
{
return view('livewire.users.create');
}
public function create()
{
$validatedData = $this->validate();
$newUserData = array_merge($validatedData, ['password' => Hash::make('temppassword')]);
//dd($newUserData);
$user = User::create($newUserData);
$user->locations()->sync($validatedData['locations']);
$this->dispatchBrowserEvent('closeModal');
$this->emit('refreshUsers');
$this->resetExcept('roles');
$this->emit('userCreated');
}
}
我尝试在我的 Livewire 组件中执行此操作:
$user->locations()->sync(array_values($newUserData['locations') ]));
但这会产生相同的结果。
I have a select2 multiple drop down menu that allows a user to select multiple locations to attach a new user to when they're creating the user.
I'm using sync()
to store the array of locations after validating the data. I'm getting the following error:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or
update a child row: a foreign key constraint fails
(location_user
, CONSTRAINTlocation_user_location_id_foreign
FOREIGN KEY (location_id
)
REFERENCESlocations
(id
)) (SQL: insert intolocation_user
(id
,location_id
,user_id
) values (1001, 0, 3))
I understand the error, as I have a database background, what I don't understand is how to get Laravel to insert the right data. the insert statement is currently:
(id, location_id, user_id) values (1001, 0, 3)
but the values are in the wrong place or being mapped incorrectly. 1001
is the location ID, but is mapped to the id
, presumably because I have id
in my array, it's also pulling in 0 presumably because it's the array key, but that's not an actual value of anything.
I want it to ignore the id field on the pivot table and just insert (location_id, user_id) values (1001, 3)
Here's what my array looks like after validation:
Here's the code in my Livewire Component:
class Create extends Component
{
public $user;
public $first_name;
public $last_name;
public $email;
public $phone;
public $role_id;
public $roles;
public $locations;
protected $rules = [
'first_name' => ['required', 'string', 'max:150'],
...
'role_id' => 'required|exists:roles,id',
'locations.*.id' => 'required|exists:locations,id'
];
public function mount()
{
$this->locations = Auth::user()->locations;
}
public function render()
{
return view('livewire.users.create');
}
public function create()
{
$validatedData = $this->validate();
$newUserData = array_merge($validatedData, ['password' => Hash::make('temppassword')]);
//dd($newUserData);
$user = User::create($newUserData);
$user->locations()->sync($validatedData['locations']);
$this->dispatchBrowserEvent('closeModal');
$this->emit('refreshUsers');
$this->resetExcept('roles');
$this->emit('userCreated');
}
}
I've tried doing this in my livewire component:
$user->locations()->sync(array_values($newUserData['locations']));
but that produces the same result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对我有用:
This worked for me: