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
1
vote
0 answers

Laravel Lumen PHPUnits [ReflectionException] Class command.laracasts.seed does not exist

I'm running a PHP project based on Laravel Lumen Whenever I run my test suite (CodeCeption framework), I run the artisan command $this->artisan("db:Seed"); at the begining of each test (in the setup method) and I get the following error: …
Amaynut
  • 4,091
  • 6
  • 39
  • 44
1
vote
3 answers

Laravel eloquent cannot determine table

I have created a database seeder to seed my table which I have created via a migration just a few seconds ago. However, I get this error when running the seeder: In Connection.php line 664: SQLSTATE[42S02]: Base table or view not found: 1146…
Jan
  • 1,180
  • 3
  • 23
  • 60
1
vote
2 answers

Laravel Seed Multiple Relations

I have 3 models: Member, Invoice and Payment Here is the Member model: class Member extends Model { public function invoice() { return $this->hasOne(Invoice::class); } } And in the Invoice model: class Invoice extends…
aronccs
  • 274
  • 3
  • 12
  • 30
1
vote
1 answer

Laravel Seeding not working

Consider the below code: foreach ($departments as $department) { factory(App\User::class, 100)->create() ->each(function ($u) use($department) { …
Raj
  • 1,928
  • 3
  • 29
  • 53
1
vote
2 answers

Seed data with relationship in Laravel

I'm trying to seed my Laravel 5.6 application through faker factory, I went through the link and little bit confused, As I have some basic static data, like for example I've a company model: class Company extends Model { use SoftDeletes,…
Nitish Kumar
  • 6,054
  • 21
  • 82
  • 148
1
vote
1 answer

Strange error when seeding Mysql database with Laravel

I'm using Laravel 5.6 and I'm using a database seeder to seed my local mysql database while developing. Here's some code from the seeder: class DatabaseSeeder extends Seeder { $products = factory(App\Product::class, 100)->create(); foreach…
flyingL123
  • 7,686
  • 11
  • 66
  • 135
1
vote
1 answer

Laravel factories being consumed by table seeders and tests. Best practices/How to do it

I'm trying to write some Laravel factories so that we can seed all tables upfront and also use them for tests. I'm not very experienced with testing and I have some doubts on which premises should factories be built. Let's use this simple example, 2…
Johny Serpa
  • 155
  • 1
  • 2
  • 16
1
vote
1 answer

How to undo a seeding action? Laravel5

I have run this seeder: private function seedUsers() { $users = [ [ 'name' => '**', 'email' => 'admin@**.com', 'gender' => '**', 'privacy' => '**', 'password' …
Seio. E.
  • 297
  • 2
  • 7
  • 18
1
vote
1 answer

QueryException "Array To String Conversion" when seeding in laravel 5.4

when I try to seed database using php artisan db:seed following exception is occured Array to string conversion (SQL: insert into users (name, email, password, remember_token, verified, verification_token, admin, updated_at, created_at) values…
Supun Ishara
  • 323
  • 2
  • 8
1
vote
3 answers

Call to undefined method Directory::create() when seeding in Laravel

I am trying to seed a database in Laravel. I am using faker to seed the database but I am getting the following error. Call to undefined method Directory::create() Below is the code I have written in the table seed file. Basically, I want to…
AltBrian
  • 2,392
  • 9
  • 29
  • 58
1
vote
1 answer

Factory model relationships in Laravel

I have 2 tables named Users and Users_meta. Both are sharing One-To-One relationship. I would like to insert dummy data with the help of seeding. I am able to do that, the only thing that is driving me crazy is that, I am unable to establish…
1
vote
1 answer

Lumen - seeder in Unit tests

I'm trying to implement unit tests in my company's project, and I'm running into some weird trouble trying to use a separate set of data in my database. As I want tests to be performed in a confined environment, I'm looking for the easiest way to…
Ours
  • 392
  • 6
  • 21
1
vote
2 answers

Seeding many-to-many relationship with model factory in laravel - column cannot be null error

Given that I have the following tables: users questions tags question_tag my pivot table with two fields: question_id & tag_id and these are my model relationships: User public function questions() { return…
Latheesan
  • 23,247
  • 32
  • 107
  • 201
1
vote
2 answers

Seeding pivot table with Laravel 5.5 model factory - mb_strtolower() expects parameter 1 to be string, array given

Given that I have the following tables: users questions tags question_tag my pivot table with two fields: question_id & tag_id and my App\Question model has the following relationships: class Question extends Model { public function user() …
Latheesan
  • 23,247
  • 32
  • 107
  • 201
1
vote
2 answers

Laravel: how to run seeder with relationships

Why "relationship" in my seeder code isn't working? It inserts to User model without any problem. But doesn't insert anything to UserMeta model. There is not even any error. I have tested this code in controller. It was functioning fine with…
Miri
  • 976
  • 1
  • 9
  • 15