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

Laravel seeding with specifically set id

I've got a data from a database as a csv file that includes ids and I need to seed these specific ids into my database. However, everytime I try to seed I get random id's assigned instead. use CsvLoader; /** * @throws \Exception …
jufg
  • 113
  • 1
  • 10
0
votes
1 answer

Laravel 8: Unable to Query Table during Seeding Using Factory

I have a seeder: class DatabaseSeeder extends Seeder { public function run(): void { $this->call([ ArticlesTableSeeder::class, ]); } } The seeder itself: class ArticlesTableSeeder extends Seeder { public…
Matt Komarnicki
  • 5,198
  • 7
  • 40
  • 92
0
votes
1 answer

How to increment variable defined at a Factory

I'm using Laravel 9 and I have made this Factory which inserts some data into a pivot table between Question && Category Models: class CategoryQuestionFactory extends Factory { /** * Define the model's default state. * * @return…
Pouya
  • 114
  • 1
  • 8
  • 36
0
votes
0 answers

Seperate Test Suites & Seeders in Laravel/PhpUnit

Is it possible to run a set collection of test suites & seeders within Laravel? For example, i have one codebase for a project which is home to different clients. Each of these clients have different requirements, so the tests are going to differ,…
JohnRugen
  • 3
  • 2
0
votes
0 answers

Seeding Related Data to Database - Laravel

I am trying to seed my e-commerce database (products, categories, etc...) with dummy data so I don't enter new data every time I refresh my db. But apparently, my skills aren't enough when it comes to relationships. anyway here are the tables I want…
0
votes
1 answer

Illuminate\Contracts\Container\BindingResolutionException : Target class [Database\Seeders\PermissionTableSeeder] does not exist

So I have a problem with seeding, the first time I got this error I thought maybe the name of the file and the table is not the same. I tried looking it up and what I found was from this link in laravel 8 with seeding , i has this issue Target class…
Gizmo Hey
  • 13
  • 5
0
votes
1 answer

Laravel retrieving wrong value from database

I'm trying to use a seeder to give, factory created, users the "membro" (member in Portuguese) role. So I have created the roles in a FuncoesSeeder (Funcoes = Functions, or roles in this context) like this: Funcao::create(['nome' => 'Administrador',…
Coisinho
  • 3
  • 2
0
votes
0 answers

Laravel 5.8 throws "Target [Illuminate\Database\Seeder] is not instantiable." on db:seed

The title basically summarises the question, but here's what I tried. database/seeds/DatabaseSeeder.php is truncated to it's bare form - no uses and the call() method is commented. Still, I cannot run the command successfully. The problem seems to…
Emil Avramov
  • 881
  • 5
  • 21
  • 38
0
votes
0 answers

Assign Roles to Multiple Users while seeding in Spatie Roles and Permissions package

I need to assign roles to multiple users while seeding. Everything works fine for single user. Code used $roleName = 'Administrator'; $role = Role::create(['name' => 'Administrator']); $permissions =…
0
votes
1 answer

Laravel seeder with relation fill only one column

I have an issue with my migrations and my seeder. I just want seed a product table with category as relations. Here my Product seeder : $product = Product::firstOrCreate( ['name' => 'Bla bla bla'], ['category_id' =>…
0
votes
1 answer

Object of class Faker\UniqueGenerator could not be converted to string

I am using laravel 9. I am trying to generate some fake users to boost my db using factoreis and seeders. When i try to seed my db i get this error Object of class Faker\UniqueGenerator could not be converted to string at…
0
votes
1 answer

Laravel 9 - Seeder | TypeError array_merge(): Argument #2 must be of type array, int given

I have two tables named Order and OrderItem. While creating data in the Order table, I am trying to enter data into the OrderItem table at the same time. The codes are as follows; OrderSeeder.php public function run() { …
0
votes
1 answer

Why doesn't this seeder class code work in production?

I have this seeder class that works/runs in my development setup: class OwnerSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { …
Adefowowe
  • 198
  • 2
  • 14
0
votes
1 answer

laravel 8: how to use seeder in unit test

im trying to get the data after seeding a simple json to database. Here's the code:
dhanyn10
  • 704
  • 1
  • 9
  • 26
0
votes
1 answer

Laravel seeder issue1

i use php artisan db:seed --class UsersSeeder in cmd and it gives me error. here is my code. can anyone help me out?