Questions tagged [laravel-seeding]

Laravel includes a simple method of seeding your database with test data using seed classes. All seed classes are stored in the database/seeds directory. Seed classes may have any name you wish but probably should follow some sensible convention, such as UsersTableSeeder, etc. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.

Laravel includes a simple method of seeding your database with test data using seed classes. All seed classes are stored in the database/seeds directory. Seed classes may have any name you wish, but probably should follow some sensible convention, such as UsersTableSeeder, etc. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.

Example Database Seed Class

class DatabaseSeeder extends Seeder {

    public function run()
    {
        $this->call('UserTableSeeder');

        $this->command->info('User table seeded!');
    }

}

class UserTableSeeder extends Seeder {

    public function run()
    {
        DB::table('users')->delete();

        User::create(['email' => 'foo@bar.com']);
    }

}

To seed your database, you may use the db:seed command on the Artisan CLI:

php artisan db:seed

By default, the db:seed command runs the DatabaseSeeder class, which may be used to call other seed classes. However, you may use the --class option to specify a specific seeder class to run individually:

php artisan db:seed --class=UserTableSeeder

You may also seed your database using the migrate:refresh command, which will also rollback and re-run all of your migrations:

php artisan migrate:refresh --seed
316 questions
54
votes
17 answers

Laravel 5.2: Unable to locate factory with name [default]

I want to seed database when I use this public function run() { $users = factory(app\User::class, 3)->create(); } Add three user in database but when I use this public function run() { $Comment= factory(app\Comment::class,…
paranoid
  • 6,799
  • 19
  • 49
  • 86
52
votes
3 answers

How to pass arguments to Laravel factories?

I have a users table and a one-to-zero/one relation with a businesses table (users.user_id => businesses.user_id). On my users table I have a discriminator which tells me if the user is of type business and therefore I need to have details on the…
Cristian
  • 2,390
  • 6
  • 27
  • 40
50
votes
6 answers

Laravel - Seeding Many-to-Many Relationship

I have a users table and a roles table that has a many-to-many relationship. These two tables are connected to a junction table called role_user. This is a model of the tables and its connections. Below are the Models in my Laravel…
Bilal Khawar
  • 511
  • 1
  • 4
  • 7
46
votes
7 answers

How to seed database migrations for laravel tests?

Laravel's documentation recommends using the DatabaseMigrations trait for migrating and rolling back the database between tests. use Illuminate\Foundation\Testing\DatabaseMigrations; class ExampleTest extends TestCase { use…
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
43
votes
9 answers

Laravel 5.1 Migration and Seeding Cannot truncate a table referenced in a foreign key constraint

I'm trying to run the migration (see below) and seed the database, but when I run php artisan migrate --seed I get this error: Migration table created successfully. Migrated: 2015_06_17_100000_create_users_table Migrated:…
mtpultz
  • 17,267
  • 22
  • 122
  • 201
42
votes
8 answers

Laravel : How do I insert the first user in the database

I am using Laravel (4.2) I am working on a project with an authentication system. I need to insert a first user into my users table. I would like to do that directly with a sql command (insert into users....). Because this first user can't be…
Dom
  • 2,984
  • 3
  • 34
  • 64
29
votes
4 answers

How to Run Laravel Database Seeder from PHPUnit Test setUp?

I am trying to recreate the database before each test in some PHPUnit test cases. I am using Laravel 5.3. Here is TestCase: class CourseTypesTest extends TestCase { public function setUp() { parent::setUp(); …
gandra404
  • 5,727
  • 10
  • 52
  • 76
27
votes
3 answers

Is it possible to prevent Laravel running Model Events when the database is being Seeded?

Laravel's seeder runs a variety of Model Events on my models which trigger New Order notification emails, among other things, from the Product::saved() Model Event. This significantly slows down database seeding. Is it possible to detect whether a…
Jack
  • 9,615
  • 18
  • 72
  • 112
26
votes
2 answers

Laravel 5.3 db:seed command simply doesn't work

I do everything by-the-book: Installed fresh Laravel 5.3.9 app (all my non-fresh apps produce the same error) run php artisan make:auth create migrations for a new table `php artisan make:migration create_quotations_table…
Peter
  • 2,634
  • 7
  • 32
  • 46
25
votes
7 answers

Passing a quantity or other arguments to a Laravel seeder

I would like to pass an argument as to define how many records I want to create during database seeding, without having to edit the factory manually. I have tried different variations on php artisan db:seed --class=UsersTableSeeder [using different…
user-44651
  • 3,924
  • 6
  • 41
  • 87
24
votes
3 answers

Defining Laravel foreign keys with Model Factories, One to One & One to Many Relationships without creating unnecessary models

Recently I have been trying to seed my database using Laravel seeding through Model Factories and Faker. For simple schemas, it is just a breeze to have it working :). However, I have encountered several problems when working with complex DB schemas…
andcl
  • 3,342
  • 7
  • 33
  • 61
21
votes
3 answers

Laravel timestamps() doesn't create CURRENT_TIMESTAMP

I have a migration that has the timestamps() method, and then I have a seed to seed this table. Schema::create('mytable', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->timestamps(); }); The…
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
20
votes
3 answers

What is the difference between Model Factory and a DB seeder in Laravel?

What is the difference between Model Factory and a DB seeder in Laravel?
Kabil
  • 261
  • 1
  • 2
  • 4
19
votes
6 answers

laravel seed rollback after seeding database

I have seeded my DB using php artisan db::seed. Is there a way to rollback what I have seeded into my DB? I cannot seem to find any command like php artisan db::seed rollback.
Aaron
  • 4,380
  • 19
  • 85
  • 141
18
votes
5 answers

How can I declare $faker in the seed file in Laravel when overriding for argument to factory?

I am trying to create multiple model of seeds like seedt1, seedt2, seedt3 with parameters for the sample. I am aware of factory states. I don't want to use it, I want to keep my factory model minimal and clean as possible. I have my model…
Lisendra
  • 183
  • 1
  • 1
  • 5
1
2 3
21 22