laravel 为数据透视表播种的更好方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23433209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Better way of Seeding a Pivot Table
提问by lozadaOmr
I have the following tables users, rolesand the pivot table role_userwith the following table structure
我有以下表用户、角色和具有以下表结构的数据透视表role_user
users
用户
- id
- username
- password
- ID
- 用户名
- 密码
roles
角色
- id
- role
- ID
- 角色
role_user
角色_用户
- id
- role_id
user_id
<?php class PivotTableSeeder extends Seeder { public function run() { // Seeds the roles table DB::table('roles')->delete(); DB::table('role_user')->insert(array( array('user_id' => 1, 'role_id' => 1), array('user_id' => 2, 'role_id' => 2), array('user_id' => 3, 'role_id' => 1), array('user_id' => 3, 'role_id' => 3), array('user_id' => 3, 'role_id' => 5) )); } }
- ID
- role_id
用户身份
<?php class PivotTableSeeder extends Seeder { public function run() { // Seeds the roles table DB::table('roles')->delete(); DB::table('role_user')->insert(array( array('user_id' => 1, 'role_id' => 1), array('user_id' => 2, 'role_id' => 2), array('user_id' => 3, 'role_id' => 1), array('user_id' => 3, 'role_id' => 3), array('user_id' => 3, 'role_id' => 5) )); } }
The pivot table is seeded using DB select
数据透视表是使用播种的 DB select
Is there a better way to seed the tables, including the pivot table?
有没有更好的方法来为表格设置种子,包括数据透视表?
I was thinking maybe when I seed my users table, it will also seed the role_user table instead of manually inserting the data into the pivot table.
我在想也许当我为我的用户表做种子时,它也会为 role_user 表做种子,而不是手动将数据插入数据透视表。
Example:
例子:
roles
角色
- id = 1
- role = encoder
- id = 2
- role = tech
- id = 3
- role = sales
- 编号 = 1
- 角色 = 编码器
- 编号 = 2
- 角色 = 技术
- 编号 = 3
- 角色 = 销售
users
用户
- id = 1
- username = user
- password = pass
- id = 2
- username = user2
- password = pass2
- 编号 = 1
- 用户名 = 用户
- 密码 = 通过
- 编号 = 2
- 用户名 = 用户 2
- 密码 = pass2
role_user
角色_用户
- id = 1
- user_id = 1
- role_id = 1
- id = 2
- user_id = 1
- role_id = 2
- id = 3
- user_id = 2
- role_id = 3
- 编号 = 1
- 用户 ID = 1
- 角色 ID = 1
- 编号 = 2
- 用户 ID = 1
- 角色 ID = 2
- 编号 = 3
- 用户 ID = 2
- 角色 ID = 3
Edit
编辑
I am seeding my users table by using this Eloquent. Is there anyway that while seeding the User, the role_user will also get updated?
我正在使用这个 Eloquent 为我的用户表做种子。无论如何,在为 User 播种时,role_user 也会更新吗?
User::create(array(
'id' => '1',
'username' => 'user',
'password' => 'pass'
));
User::create(array(
'id' => '2',
'username' => 'user2',
'password' => 'pass2'
));
回答by The Alpha
You may try this (Assuming that you have already seeded roles):
你可以试试这个(假设你已经播种了角色):
$user = User::create(['id' => '1', 'username' => 'user', 'password' => 'pass']);
$user->roles()->sync([1,2]); // array of role ids
$user = User::create(['id' => '2', 'username' => 'user2', 'password' => 'pass2']);
$user->roles()->sync([3,4]); // array of role ids
I've used []
instead of array()
, if your PHP
is prior to ver-5.4
then you should use array()
.
我使用了[]
代替array()
,如果您PHP
在此之前使用,ver-5.4
则应该使用array()
.
回答by Jarek Tkaczyk
For testing purposes I use pretty simple and fast method like below.
出于测试目的,我使用了如下所示的非常简单快速的方法。
Imagine we have users and categories with pivot table (this comes from JeffreyWay's generators btw):
想象一下,我们有带有数据透视表的用户和类别(顺便说一下,这来自 JeffreyWay 的生成器):
<?php
// Composer: "fzaninotto/faker": "v1.3.0"
use Faker\Factory as Faker;
class UsersTableSeeder extends Seeder {
public function run()
{
$faker = Faker::create();
foreach(range(1, 100) as $index)
{
User::create([
'username' => $username = $faker->userName,
'email' => $faker->email,
'password' => Hash::make($username),
'account_id' => $index
]);
}
}
}
// all other seeders look the same, so I paste just the code that matters:
// Categories
foreach(range(1, 30) as $index)
{
Category::create([
'name' => $faker->word
]);
}
// pivot table
foreach(range(1, 50) as $index)
{
DB::table('category_user')->insert([
'category_id' => rand(1,30),
'user_id' => $faker->unique()->randomNumber(1, 100)
]);
}
回答by ollieread
I'm not sure how others feel about this, but when seeding the database, I like to use the actual models for the seeding, that way you can use all the handy functions that come with inserting related models.
我不确定其他人对此有何看法,但是在为数据库播种时,我喜欢使用实际模型进行播种,这样您就可以使用插入相关模型所带来的所有便捷功能。
If you're seeding the user and roles with predefined ids, then it's easy.
如果您使用预定义的 ID 为用户和角色设置种子,那么这很容易。
class UserSeeder extends Seeder
{
public function run()
{
DB::table('users')->truncate();
DB::table('roles')->truncate();
DB::table('user_roles')->truncate();
$users = [];
$user = User::create(['id' => 1, 'blah' => 'honk']);
$users[$user->id] = $user;
$user = User::create(['id' => 2, 'blah' => 'honk']);
$users[$user->id] = $user;
// etc etc
$roles = [];
$role = Role::create(['id' => 1, 'blah' => 'honk']);
$roles[$role->id] = $role;
// etc etc
$user[1]->roles()->sync(1);
$user[2]->roles()->sync(2);
$user[3]->roles()->sync(1, 3, 5);
}
}
In a recent application I wrote, I created a seeder that created the ACL groups, then permissions, assigned the permissions to the specific groups, then randomly created somewhere between 10-30 (using rand
) users and randomly assigned them to different groups.
在我最近编写的一个应用程序中,我创建了一个播种机,它创建了 ACL 组,然后是权限,将权限分配给特定的组,然后随机创建 10-30 个(使用rand
)用户并将它们随机分配到不同的组。
Let me know if anything is unclear.
如果有任何不清楚的地方,请告诉我。
回答by Ben
For n:m Relationships, where I need to attach random entries, I use this simple but efficient Seeder code, that only uses real ids:
对于 n:m 关系,我需要附加随机条目,我使用这个简单但高效的 Seeder 代码,它只使用真实的 id:
$faker = Faker\Factory::create();
$limit = 100;
for ($i = 0; $i < $limit; $i++) {
$newrow = *Yourmodel*::create ([
'email' => $faker->word . rand(0, 9999) . '@test.com' ,
...
]);
$ids = $faker->randomElements( \App\YourOtherModel::select('id')->get()->toArray(), rand(1,*3*) );
foreach($ids as $id) {
$newrow->*your_relationship*()->attach( $id );
}