Laravel Livewire 同步数据透视表

发布于 2025-01-11 15:14:46 字数 2272 浏览 0 评论 0原文

我有一个 select2 多个下拉菜单,允许用户在创建用户时选择多个位置来附加新用户。

我在验证数据后使用 sync() 来存储位置数组。我收到以下错误:

SQLSTATE[23000]:违反完整性约束:1452 无法添加或 更新子行:外键约束失败 (location_user,约束 location_user_location_id_foreign 外键 (location_id) REFERENCES locations (id)) (SQL:插入 location_user (idlocation_iduser_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, CONSTRAINT
location_user_location_id_foreign FOREIGN KEY (location_id)
REFERENCES locations (id)) (SQL: insert into location_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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疾风者 2025-01-18 15:14:46

这对我有用:

$locations = Arr::flatten($newUserData['locations']);
$user->locations()->sync($locations);

This worked for me:

$locations = Arr::flatten($newUserData['locations']);
$user->locations()->sync($locations);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文