Database Seeding

Seeding is helpful when you wish to populate your database easily either for initial data, or to patch up data for your database.

Seeding Your Database

Before we can start, we need to create our seed:

$ php mighty make:seed seed_user

The above command will create a seed file under Database/Seeds . You will come to see the template as such:

class seed_user{
public function plant(){
}
}

If you would like to add an admin user into your database, your code may look like this. Assuming you have a UsersModel:

use \Application\Models\UsersModel;
class seed_user{
public function plant(){
$date = date('Y-m-d');
$password = password_hash('admin', PASSWORD_DEFAULT);
$user = new UsersModel();
$user->insert([
"users_email" => "[email protected]",
"users_password" => $password,
"users_createdAt" => $date,
"users_modifiedAt" => $date
]);
}
}

Once you are ready with the seed file. Run it by running:

$ php mighty seed:plant