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 5.2 - How to get the SQL generated by your seeds

I have some seeds and I simply want to see the SQL they generate. With migrations I would simply do --pretend but this isn't an option for db:seed. I also realize that I can see all the SQL statements that I run by using DB::getQueryLog() etc.…
Bill Garrison
  • 2,226
  • 3
  • 34
  • 75
1
vote
1 answer

Laravel 5.0.34 database seeding error with tinker

I am following this laracast tutorial and as I key in this command in tinker: factory('App\Flyer')->make(); I get this error: PHP Fatal error: Call to undefined function factory() in eval()'d cod1 My php version locally installed is 5.4.38 So…
rahulserver
  • 10,411
  • 24
  • 90
  • 164
1
vote
1 answer

Laravel 4 Database insert Error - preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

In database seeder, I just want to insert some hard-coded data to the table. $Pro1_id = DB::table('projects')->select('id')->where('projectName', '=', 'Project A')->get(); $data1_1 = array( 'id' => 1, …
Matilda Yi Pan
  • 658
  • 2
  • 8
  • 26
1
vote
1 answer

Laravel 5: class 'Illuminate\Database\Seeder' not found

Problem I'm currently facing to has been posted here already, yet none of them could solve my one. I'm talking about database seeder located under url like http://HOSTNAME/laravelfiles/database/seeds/UsersTableSeeder.php. Its content is as…
1
vote
1 answer

Laravel 5.1: Factory/Seeder Pivot Tables

I ma trying to create soem seeder data for a pivot table. The code below works to one point that it will hit a duplication error. Is there a way to do this better or to improve this code so not to cause duplication. …
Simon Davies
  • 3,668
  • 9
  • 41
  • 69
1
vote
0 answers

Costly process re-seeding production bad data with Laravel in large MySQL dataset

I'm looking for a saner way to correct errors in a large seeded database. Solutions are welcome, the saner they are (feel free to question the tools for the job i.e. relational database). I can't afford to truncate the data and reseed all values.…
0
votes
1 answer

Laravel one to many table seed is not seeded as expected

So I have a relationship table like this ERD. I'm using eloquent database relationship in laravel to manage the foreign key. It worked, but the data added is skipping one row. This is how I seed the database. public function run(): void { …
cherno304
  • 27
  • 5
0
votes
1 answer

seeding static data in laravel 10 error argument #1 must be of type array

I'm trying to seed a static data (existing categories) to a 'categories' table in Laravel 10. I'm very new to Laravel so I'm confused even after reading the documentation and tutorial videos. This is my code: categories table schema from the…
dapidmini
  • 1,490
  • 2
  • 23
  • 46
0
votes
0 answers

Manual Seeader for the parent and factory for the child in one-to-many relationship in Laravel

I am wondering if this is possible. I have ProgrammeSeeder seeding the manual array of programs. Then I have the factory for age groups. I have drafted the code blocks below. I don't want to use a factory for the Programme class as programs are…
Charix
  • 29
  • 3
0
votes
1 answer

Laravel Factories

i have 2 tables user and roles, i want to create a user factory so that i add an admin role for that user created. I have the user table with (id,name,...,age ) and the roles table with (id, user_id and role_name). i want to make a factory that…
0
votes
0 answers

unable to seed the database in laravel

I’m creating a blog in Laravel framework everything was going right until I tried to seed the database, I type this command in two ways and the following errors appear c:\wamp64\www\my-blog>\App\Models\BlogPost::factory()->times(10)->create(); The…
haya
  • 1
  • 1
0
votes
1 answer

How can I Seed a Image using Laravel Seeder and store it in the Database?

I Am trying to insert a image in the Database using seeder but its not responding properly. I have to insert a image in the database from the seeders for my view process. the image should be first view there is no create condition you can assume…
0
votes
0 answers

Seeders with multi-level nested each functions generate more results than expected

In the hope that someone could shed some light on the following: A user has an account and an account has associated cards with it. I am trying to make a seeder to create 5 users with 1 account each of them and then each account to have 1 card…
aagian
  • 1
  • 2
0
votes
1 answer

Laravel seeding - Array to string conversion

I am having this seeder
ltdev
  • 4,037
  • 20
  • 69
  • 129
0
votes
2 answers

Laravel Seeder throws SQLSTATE[42000]: Syntax error or access violation

I am having the following db tables // Table 1: Foos id, foo_name, foo_type, created_at, updated_at // Table 2: Bars id, bar_name, bar_type, parent_id, foo_id [ForeignKey], created_at, updated_at // Table 3: Quxes id, qux_name, bar_id…
ltdev
  • 4,037
  • 20
  • 69
  • 129