1
我有一个用户表和一个帐户表。Laravel 4 - 将多个值插入数据透视表
此帐户表定义了用户可以拥有哪些帐户。
例如
账户:
id| name
1 | library
2 | school
3 | maths
用户:
id| username
1 | username1
2 | username2
我现在有另一个表:users_accounts
user_id | account_id
我想提出一个接口,用于创造新的用户。在这个页面上,我有基本的细节,但也有一个复选框列表(从帐户表创建)。复选框表示用户需要为其设置的帐户。
我想要做的就是创建账户时:
$user = User::create($input);
我也想添加的用户ID,他们需要建立到users_accounts表中的账户。单个用户可以拥有多个帐户。
我可以使用数据透视表和belongsToMany来做到这一点吗?
在长期来看:
$user = User::create($input);
loop through the checkboxes {
add a new row into the user_accounts table with the ticked checkboxes
}