
What is Database Seeding?
Database seeding is the process of seeding a database with actual or dummy data, according to Wikipedia. Seeding a database is the process of providing an initial collection of data to a database after setup. It's especially beneficial when we need to populate the database with data that we'll need later.
Why is database Seeding required?
Database Seeding is required when we went to add testing data into our application which looks exactly entered by a user. Testing is another major use of database seeding in Laravel.
Suppose one of our application's functionality is searching, so whenever the user enters a query it will show a few results ranked from thousands of data from our database. In searching applications, response time plays a major role. So for testing response time to search thousands of records we need to add those records to our database. Manually adding those data can be time taking and stressful but with database seeding, it becomes easier. In addition, we can control data duplicity and data type validation.
Laravel includes the ability to seed your database with data using seed classes. All seed classes are stored in the database/seeders directory. The database\seeders\DatabseSeeder.php helps us to manage the order of database seeding into Laravel.
Creating Model and Migration
Laravel is MVC Framework and Model is fundamentally the base class for application logic. In Laravel application, the Model communicates with the database and responsible for each piece of data.
Let's make a model with migration for posts with the below command.
Here, we are planning for a mass assignment so we need to add our fields into a fillable array.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = [
'title',
'description',
'date',
'name'
];
}
Open the database/migrations/timestamp_posts_table.php file and let's define the database structure for the posts table.
Here, we have defined a database table structure just to save the post title, description, date, user, and timestamps. In actual model, you can add any number of field as per your requirement.
To run migration execute the given below command into a terminal:
It will generate a database table by name posts.
Create Factory
Here, we will create seeder with dummy data so we will use factory and faker methods to seed data. The factory is very useful while testing for creating multiple data at a time. Factories return a default set of data by a defined datatype.
To create a factory hit the below command into a terminal :
With this command, you will get a new file database\factories\PostFactory.php file. Let's modify it as per our requirements.
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class PostFactory extends Factory
{
public function definition()
{
return [
'title' => $this->faker->sentence(),
'description' => $this->faker->text(),
'date' => $this->faker->date(),
'user' => $this->faker->sentence(),
];
}
}
the Faker PHP library, which allows you to conveniently generate various kinds of random data for testing. Here, we have used some faker methods to get appropriate data.
Create Seeder
In this step, we will create a seeder for post model and configure factory to create specific number of records to database. Enter below command to create Seeder :
The above command will create database/seeders/PostSeeder.php file into your application. Let's make some changes to call model factory for creating 100 posts.
<?php
namespace Database\Seeders;
use App\Models\Post;
use Illuminate\Database\Seeder;
class PostSeeder extends Seeder
{
public function run()
{
Post::factory()
->count(100)
->create();
}
}
Define Seeder into Database Seeder
While using multiple seeders in our application, sometimes we need to set a hierarchy. With DatabaseSeeder you can set which seeder will execute first and in which order.
With Database Seeder you don't need to seed each and every class individually. You can configure all seeders into a database seeder and it will execute.
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call([
PostSeeder::class,
]);
}
}
Seeding Database Using Seeders
Finally, We have created our seeder and it's time to seed our database. Enter below command into terminal :
It will seed all your seeders defined in your DatabaseSeeder.
You can seed also seed a single seeder using the below command
In some cases, you need to create a fresh database with seeding then you can use the below command :
Sometimes after creating a new seeder, you face an error. So you might require to regenerate Composer’s autoloader.:
Conclusion
Here we have taken examples for post table and seed data into it. In the actual product, you need to seed fixed data to our application when you can use Laravel's database seeding functionality and seed those data with simple commands.
In this example, we used Faker for generating data but you can use CSV, JSON files, excel files, XML data, Arrays, or any other format of data using Seeders. You just need to alter the reading logic for those data.