Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is database seeding in Laravel?

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

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?

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 seed in database field.

like image 349
stack Avatar asked Dec 18 '16 06:12

stack


People also ask

What is meant by seeding a database?

Data seeding is the process of populating a database with an initial set of data. There are several ways this can be accomplished in EF Core: Model seed data. Manual migration customization. Custom initialization logic.

Why do we seed a database?

Seeding allows you to consistently re-create the same data in your database and can be used to: Populate your database with data that is required for your application to start - for example, a default language or a default currency.

What is seed and factory in Laravel?

Database seeder is used to populate tables with data. Model factories is a convenient centralized place to define how your models should be populated with fake data.

What is seeding in development?

Seed development is a pivotal process in the life cycle of angiosperms. It is initiated by the double fertilization which leads to the development of the embryo and the endosperm. Among angiosperms, monocotyledonous and dicotyledonous seed development have important similarities and differences.


2 Answers

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):

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->name will generate a random person name, and $faker->email – a random email. After running command php artisan db:seed your database get populated with some random entries.

You can find this package inside your composer.json file under 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->randomDigit;
$faker->numberBetween(1,100);
$faker->word;
$faker->paragraph;
$faker->lastName;
$faker->city;
$faker->year;
$faker->domainName;
$faker->creditCardNumber;

Hope this helps!

like image 84
Saumya Rastogi Avatar answered Oct 06 '22 00:10

Saumya Rastogi


Usually you're using model Factories and faker to 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 package to create seeder from real data.

You may want to read the docs on seeding.

like image 27
Alexey Mezenin Avatar answered Oct 06 '22 00:10

Alexey Mezenin