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
4
votes
2 answers

How to seed self referencing table Laravel 5.1

How can I seed a table with two foreign keys from the same table like messages: Migration: public function up() { Schema::create('messages', function (Blueprint $table) { $table->increments('id'); …
Angad Dubey
  • 5,067
  • 7
  • 30
  • 51
4
votes
3 answers

Laravel :"php artisan db:seed" doesn't work

I am try to run "ServiceTableSeeder" table in database i got an error msg. I try run "php artisan db:seed" Msg: [symfony\component|Debug\Exception\FetalErrorException] cannot redeclare DatabaseSeeder::run() DatabaseSeeder .php
CodeMan
  • 1,941
  • 6
  • 26
  • 44
3
votes
1 answer

Run Laravel Seeder From Package

I'm working on a package for an internal Laravel application and I'm having trouble running a seeder that exists in the package directory. In my package's composer.json file located in packages/vendor/packagename, I've added the…
Josh
  • 714
  • 2
  • 8
  • 20
3
votes
1 answer

How to pass parameters to laravel factory in database seeder?

Is it possible to pass data from seeder to factory? This is my PictureFactory: class PictureFactory extends Factory{ protected $model = Picture::class; public function definition($galleryId = null, $news = false){ if…
3
votes
2 answers

Getting Column not found Error at Illuminate\Database\QueryException?

I am working in Laravel app and relating users table with posts table by user_id column. I already have user_id column in posts table. But when I run php artisan db:seed command, I get an error : `SQLSTATE[42S22]: Column not found: 1054 Unknown…
Abhishek T.
  • 1,133
  • 1
  • 17
  • 33
3
votes
4 answers

laravel database seeder adding add foreign key id randomly to seeds

I am trying to create seeders for testing purposes. I have users that belongs to a room via a room id, these rooms are created via a room seeder, in my users seeder, I create a user and update the room_id attribute like this,…
Udders
  • 6,914
  • 24
  • 102
  • 194
3
votes
1 answer

Laravel Seeder - Update all records with a Unique ID (UUID)

I have just added a UUID for my users table. I want to update all the records with a UUID that is unique. In my seeder I have the following code that generates a UUID and populates this new column in the database. However, the generated UUID is the…
ejntaylor
  • 1,900
  • 1
  • 25
  • 43
3
votes
1 answer

How to populate a nested category table with Faker in Laravel?

I want to put the subcategory id into quote table but it seems too complicated! This is my try, obviously it ended up with a loop. Here is the seeder: public function run() { factory(App\User::class, 50)->create()->each(function ($u) { …
Mojdeh
  • 33
  • 1
  • 3
3
votes
1 answer

how to run unittests in laravel 5.5 using a sqlite in memory database

I setup a test database: phpunit.xml:
steros
  • 1,794
  • 2
  • 26
  • 60
3
votes
1 answer

Set other columns to same type depending on a column value set - Laravel Faker

I'm using Faker in my Laravel 5.5 project to fake some data. I ran into this issue where I need to set two other columns inside the faker to the same value as the "live" column that was generated. Here is what I…
David
  • 1,987
  • 5
  • 33
  • 65
3
votes
2 answers

Seeding relational data in Laravel eloquent database seeder / factory - Type error - Arg 1 should be Eloquent\Model - Collection given

I am trying to seed a table with relational data in a Laravel 5.5 application. I have these two tables/models: User Question On the app\User.php model file; I have the following hasMany relationship: public function questions() { return…
Latheesan
  • 23,247
  • 32
  • 107
  • 201
3
votes
1 answer

Using model count in Laravel factory

I have a one to many relationship between User and Post. I am using a database seeder and factory to generate fake data. I would like to produce similar data to the following tables. User user_id | name ------- | ------ 1 | Tim Post post_id…
Marc Barbeau
  • 826
  • 10
  • 21
3
votes
3 answers

Laravel 5 - How to seed table automatically after deploying an app?

I have a certain data that must be in table in order for my app to work, otherwise i get an error message. For example if YOU or anyone else pulled my app from github and if you run php artisan migrate and then try to test the app you would get an…
lewis4u
  • 14,256
  • 18
  • 107
  • 148
3
votes
3 answers

Laravel 5.1 - Model Factory Error seed

Hi i'm trying to populate my database with some Articles, BlogCategories, Users. when i do "php artisan db:seed" i have this error: [ErrorException] Argument 2 passed to Illuminate\Database\Eloquent\Factory::define() must be callable. string…
Diego Cespedes
  • 1,353
  • 4
  • 26
  • 47
3
votes
0 answers

Laravel model factory make method with created_at and updated_at

So I was working with laravel seeds and came across this amazing article on how to speed them up. http://bensmith.io/speeding-up-laravel-seeders I have tested this method and yes the seed time has dropped to half for my most my seeds. The only…
Rohan
  • 13,308
  • 21
  • 81
  • 154