Laravel 中的数据库播种是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41206038/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:56:49  来源:igfitidea点击:

What is database seeding in Laravel?

phplaravellaravel-seeding

提问by stack

I use Laravel framework and I have recently been informed that there is something named database seedingwhich produces a fake dataset for our tests. Is my understanding correct?

我使用 Laravel 框架,最近我被告知有一个名为的东西database seeding会为我们的测试生成一个假数据集。我的理解正确吗?

Well that's pretty much odd. How it works? How it knows which type of data do I need in X column of database? And how it generates it?

嗯,这很奇怪。这个怎么运作?它如何知道我需要在数据库的 X 列中使用哪种类型的数据?以及它是如何产生的?

Also, Can't I make a seed of my real dataset (something like an export)? You know, I don't know English very well, that's why I cannot understand the concept of seedin database field.

另外,我不能制作我真实数据集的种子(类似于导出)吗?你知道,我不太懂英语,所以我无法理解数据库领域种子的概念。

采纳答案by Alexey Mezenin

Usually you're using model Factoriesand fakerto create fake data (with relations etc) for developing and testing your app.

通常你使用模型工厂摊贩,以制造假数据(与关系等),用于开发和测试您的应用程序。

If you want to seed real data, just use commands to import dump. Or, if your data is something like table with countries, create seeder which inserts the real data without using faker or model factory.

如果要播种真实数据,只需使用命令导入转储即可。或者,如果您的数据类似于带有国家/地区的表格,请创建播种机,在不使用伪造者或模型工厂的情况下插入真实数据。

Also, you can use some packageto create seeder from real data.

此外,您可以使用一些包从真实数据创建播种机。

You may want to read the docs on seeding.

您可能想阅读有关播种文档

回答by Saumya Rastogi

Yes, Laravel comes with the very great & popular package named - Faker. You can write this example, using Faker and generate 10 users like this (inside DatabaseSeeder.php):

是的,Laravel 附带了一个非常棒且流行的包,名为 - Faker。您可以使用 Faker 编写此示例并生成 10 个这样的用户(内部DatabaseSeeder.php):

use DB;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

use Faker\Factory as Faker;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $faker = Faker::create();
        foreach (range(1,10) as $index) {
            DB::table('users')->insert([
                'name' => $faker->name,
                'email' => $faker->email,
                'password' => bcrypt('secret'),
            ]);
        }
    }
}

That's it – $faker->namewill generate a random person name, and $faker->email– a random email. After running command php artisan db:seedyour database get populated with some random entries.

就是这样 -$faker->name将生成一个随机的人名,以及$faker->email- 一个随机的电子邮件。运行命令后,php artisan db:seed您的数据库将填充一些随机条目。

You can find this package inside your composer.jsonfile under require-dev:

你可以在你的composer.json文件中找到这个包require-dev

"require-dev": {
    "fzaninotto/faker": "^1.6", // <------- here
    "mockery/mockery": "0.9.*",
    "phpunit/phpunit": "~5.0",
    "symfony/css-selector": "3.1.*",
    "symfony/dom-crawler": "3.1.*",
    "laracasts/testdummy": "~2.0"
},

Faker can generate lots of data, from which some are given below:

Faker 可以生成大量数据,其中一些数据如下:

$faker->randomDigit;
$faker->numberBetween(1,100);
$faker->word;
$faker->paragraph;
$faker->lastName;
$faker->city;
$faker->year;
$faker->domainName;
$faker->creditCardNumber;

Hope this helps!

希望这可以帮助!