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
0
votes
1 answer

error when seeding data into a database in laravel 8

am getting this error when seeding data into my database. apparently the I have 2 models bookings and bookingstatus model.the bookingstatus seeder is working well and is seeding the status very well but when it comes to the bookings the data is…
0
votes
0 answers

How to seed pivot table that has field?

I have a number in pivot table and I don't know how I can set the value in number. The task of number filed in pivot table : Specifies the order of the video in the…
Mesh
  • 1
  • 1
  • 1
0
votes
1 answer

Error when runing faker at seeder laravel 8 Undefined constant "App\Models\boolean"

I get an error when create a table seeder using model factory in laravel 8 but I don't know where I'm going wrong here. This is an error: Undefined constant "App\Models\boolean" at…
0
votes
1 answer

Illuminate\Contracts\Container\BindingResolutionException Target class [HotelSeeder] does not exist

I am trying to use seeder following a tutorial but I am always getting this error Illuminate\Contracts\Container\BindingResolutionException Target class [HotelSeeder] does not exist. when I run this command : php artisan db:seed I have in…
0
votes
1 answer

How to fix upsert problem when seeding? (laravel)

I have these code below, all seems working but when I try to run unit test it returns an error below. Here is my seeder (this seeder is called many times in different test cases): DB::table('sizes')->upsert([ [ 'name' => 'jumbo', …
schutte
  • 1,949
  • 7
  • 25
  • 45
0
votes
2 answers

Larvel 8 :- Array to string conversion

I get the title error when I run command: php artisan db:seed. I want to store data in database. migration is already don. but when seeding it shows and error. I have no idea where this problem comes from. ProductFactory.php
Muqadar Ali
  • 57
  • 2
  • 11
0
votes
2 answers

How to pass null argument to Laravel Factory from the seeder?

I've got a Factory definition like this: public function definition() { $to = $this->faker->numberBetween(1, 3); return [ "ad_id" => $to == 1 ? Ad::all()->random()->id : null, "delivery_id" => $to == 2 ?…
Carlos Tinnelly
  • 503
  • 1
  • 3
  • 20
0
votes
1 answer

How to prevent the duplication in one-to-many relationship?

I have 2 tables named Company and Customer. One-to-Many relationship, where a company can have many customers and a customer belongs to this company. Company Table Customers Table Company Id, Customer_ID, …
0
votes
1 answer

Seeding error: Object of class DateTime could not be converted to string

I'm seeding my database with laravel but i get this error. To remove it i need to comment the mutator part of the model public function setBirthdayAttribute($value) { $this->attributes['birthday'] =…
netnull
  • 201
  • 1
  • 3
  • 13
0
votes
1 answer

Laravel 8 Seeding : run:handelerror

I am trying to follow this documentation on how to make a seeder work https://laravel.com/docs/8.x/seeding#writing-seeders Essentially just trying to get the demo thing they have working. When I run php artisan db:seed I get this…
Jakub
  • 1,260
  • 1
  • 13
  • 40
0
votes
3 answers

laravel seeder is giving an error and won't seed

I've been trying to get my seeder to work but it keeps giving me the following error Call to undefined function Database\Seeders\factory() at database/seeders/ContactTableSeeder.php:16 12▕ * @return void 13▕ */ 14▕ …
CasaCoding
  • 119
  • 1
  • 2
  • 15
0
votes
1 answer

Laravel artisan db:seed not seeding new record added to the Seeder class

I had some record in table inserted by SomeTableSeeder, I have deleted records from table and updated the SomeTableSeeder (added one more record). When I am running the php artisan db:seed --class=SomeTableSeeder, the new record I added later is not…
AbhimanuSharma
  • 453
  • 1
  • 8
  • 21
0
votes
1 answer

I need to use raw sql data as seeder

I have this SQL data and I want to use it as seeder. Now my Team Lead said "That's a dump", additionally he said that I can use this sql data as SEEDER, but first I need to do something before I can use as seeder. Can someone here knows what exactly…
0
votes
1 answer

error when attempting to seed pivot table using Laravel

I have just created a many-to-many relationship between the models Project and Features using Laravel however I receive the following error when attempting to run the seed script. SQLSTATE[42S02]: Base table or view not found: 1146 Table…
cinameng
  • 313
  • 4
  • 14
0
votes
0 answers

Laravel Seeder Stuck no progress

I have this on my DatabaseSeeder $this->call(RolesTableSeeder::class); $this->call(UsersTableSeeder::class); $this->call(MemberSeeder::class); $this->call(MemberUserSeeder::class); I tried it once and successfully done but after…
EasyWay
  • 365
  • 1
  • 5
  • 20