What is database sowing in Laravel?

I use the Laravel framework, and I was recently informed that there is something called database seedingthat creates a fake dataset for our tests. Do I understand correctly?

Well, that's pretty weird. How it works? How does he know what data type I need in the X column of the database? And how does it generate it?

Also, can I not make the seed of my real dataset (something like export)? You know, I don’t know English very well, so I can’t understand the concept of seed in the database field.

+4
source share
2 answers

faker ( ..) .

, . , , , factory.

, .

docs .

+3

, 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'),
            ]);
        }
    }
}

Thats it - $faker->name , $faker->email - . php artisan db:seed .

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

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

, !

+5

Source: https://habr.com/ru/post/1664081/


All Articles