Follow previous example of creating a seed. This example uses a MySQL Dump to seed a table in the project database. The table must be created before seeding.
<?php
use Illuminate\Database\Seeder;
class UserTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$sql = file_get_contents(database_path() . '/seeds/users.sql');
DB::statement($sql);
}
}
Our $sql is going to be the contents of our users.sql dump. The dump should have an INSERT INTO statement. It will be up to you where you store your dumps. In the above example, it is stored in the project directory \database\seeds
. Using laravel's helper function database_path()
and appending the directory and file name of the dump.
INSERT INTO `users` (`id`, `name`, `email`, `password`, `remember_token`, `created_at`, `updated_at`) VALUES
(1, 'Jane', '[email protected]', 'superSecret', NULL, '2016-07-21 00:00:00', '2016-07-21 00:00:00'),
(2, 'John', '[email protected]', 'sup3rS3cr3t', NULL, '2016-07-21 00:00:00', '2016-07-21 00:00:00');
DB::statement($sql)
will execute the inserts once the Seeder is run. As in previous examples, you can put the UserTableSeeder
in the DatabaseSeeder
class provided by laravel:
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->call(UserTableSeeder::class);
}
}
and run from CLI in project directory php artisan db:seed
. Or you can run the Seeder for a single class using php artisan db:seed --class=UsersTableSeeder