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
257 questions
50
votes
14 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
  • 5,453
  • 14
  • 38
  • 70
37
votes
2 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
  • 1,919
  • 3
  • 21
  • 33
34
votes
4 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
  • 351
  • 1
  • 3
  • 7
29
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
  • 28,726
  • 15
  • 96
  • 149
29
votes
7 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
  • 1,984
  • 2
  • 19
  • 41
29
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
  • 13,197
  • 18
  • 96
  • 180
23
votes
2 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,133
  • 15
  • 64
  • 109
22
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,415
  • 4
  • 25
  • 38
20
votes
3 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
  • 4,515
  • 9
  • 42
  • 62
17
votes
4 answers

How to implement your own Faker provider in Laravel

I want to create a custom provider for Faker in Laravel (e.g. one for a random building name). Where do I store the custom provider in my application and how do I use it?
carte
  • 983
  • 2
  • 9
  • 27
16
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
  • 27,770
  • 29
  • 134
  • 260
12
votes
2 answers

Laravel seeding results in Null timestamp

So i had a seeder for Languages Table (LanguageTableSeeder) as follows: DB::table('languages')->insert([ 'name' => 'English', 'flag' => '', 'abbr' => 'en', 'script' => 'Latn', 'native' => 'English', …
Jhivan
  • 155
  • 1
  • 1
  • 8
12
votes
2 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
  • 2,473
  • 3
  • 25
  • 49
12
votes
4 answers

A four digit year could not be found Data missing

I'm trying to seed using factories in Laravel 5.2 My code dies in the User factory: $factory->define(App\User::class, function (Faker\Generator $faker) { $countries = Countries::all()->pluck('id')->toArray(); return [ 'name' => $faker->name, …
Juliatzin
  • 14,437
  • 26
  • 115
  • 241
11
votes
6 answers

How to run laravel migration and DB seeder except one

I am having many migration and seeder files to run, Although I will need to run all files but currently I need to skip one migration and seeder. How could I skip one file from laravel migration and db seeder command. I do not want to delete files…
Niklesh Raut
  • 29,238
  • 9
  • 61
  • 94
1
2 3
17 18