The introduction
In the previous chapter, we learned about creating database tables using laravel migration, which saves jobs by moving DBA jobs to the development side. But with bare database tables, a shell with no data really doesn’t help. Here’s how Laravel faked data into a database and pretended to produce it.
The construction method used in this article is to write dirty data, messy data, no meaningful data, only useful for applications and databases, nothing else!
Code time
It is just like planting a field with seeds, which can grow crops and food; And there’s no end to grandchildren. The same goes for databases. A database table is like a field. To plant a field, we need seeds to use it at the command line:
php artisan migrate --seed
Copy the code
Or start the database from scratch and empty the old data and populate it:
php artisan migrate:fresh --seed
Copy the code
This is for all migrations and all the seed files. If you specify a separate seed file, you can do this:
php artisan db:seed
php artisan db:seed --class=VotesTableSeeder
Copy the code
Now that we have the ground ready in the previous chapter, let’s prepare a bag of seeds using the command line scaffolding command:
php artisan make:seeder ContactsTableSeeder
Copy the code
The above instruction will be generated file * * database/seeds/ContactsTableSeeder PHP * *.
In order to introduce the seed file to use, in the database/seeds/DatabaseSeeder. PHP file add the following code:
public function run()
{
$this->call(ContactsTableSeeder::class);
}
Copy the code
Then manually implement the Run () method of the ContactsTableSeeder class:
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class ContactsTableSeeder extends Seeder
{
public function run()
{
DB::table('contacts')->insert([
'name'= >'Tom Hanks'
'email'= >'[email protected]',]); }}Copy the code
You should be familiar with the native database CRUD operation, which specifies the name of the table and inserts a new piece of data. It’s that simple. It’s just one move. However, the above way, can only create a data, we want to fabricate, how also have to fabricate large-scale, large-scale data bar.
Model factory
Say a lofty concept, sounds lofty, actually uses extremely simple.
Model factory!
What is this? It’s a factory that makes models. A model is like a mold. With a mold, thousands of products can be processed in a factory. A model factory is such a use.
Again using the command line scaffolding create:
php artisan make:factory ContactFactory --model=Contact
Copy the code
The above directive creates a PHP file in the Database/Factories directory called contactFactory.php, which we specify produces data for the Contact model file corresponding to the contacts table.
Add the following code to the file:
use APP\Contact;
use Faker\Generator as Faker;
$factory->define(Contact::class, function (Faker\Generator $faker) {
return [
'name'= >$faker->name,
'email'= >$faker->email,
];
});
Copy the code
Why does this file look like this? Why do you write that? Class is not class, method is not method, function is not function! That’s right, it’s a code snippet, laravel convention thing, you write it this way, and it works when you perform data populating and testing! It’s amazing.
So how do you control the number of fillings? Just call the factory class where you need to populate the data:
$contact = factory(Contact::class)->create();
Copy the code
This is a single piece of data that can be specified to create 500:
factory(Contact::class, 500)->create();
Copy the code
We create the model factory class ContactFactory, use the Factory shortcut to instantiate the call, and make N calls to each model using data from the factory class.
OK, model factory has a lot of fancy gameplay, so I’ll leave it there for this article.
Write in the last
This article introduces the use of Laravel’s Seeder for data populating. For efficient populating, we introduce factory mode, an advanced gadget that is a bit out of the box. I hope you enjoyed it.
Happy coding 🙂
I am a @programmer assistant, an original author in the IT field who focuses on programming knowledge and circle dynamics