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

Is laravel seeding as hard as I see it or I'm stumbling with something that's not that complicated?

The last few days I've been rocking my head against the wall with the seeders. I can't seem to get the hang of it. The relationships are very simple: A Brand hasMany products and each product belongs to a single brand. A Category hasMany products…
Alberto
  • 61
  • 1
  • 9
0
votes
1 answer

Seeding table with multiple Relations laravel

Hello i'm new to laravel and I want to seed a table with multiple relations like this for ($i=0; $i < 30; $i++) { Product::create([ 'title' => $faker->sentence(1), 'slug' => $faker->slug, …
Firas
  • 23
  • 3
0
votes
1 answer

Laravel seeder raise 'Column not found' about dummy column

I have a User model with custom attribute, attribute name that concatenate firstName and lastName: class User extends Authenticatable { /** * @var array */ protected $fillable = ['firstName', 'lastName']; protected $appends =…
0
votes
1 answer

php artisan migrate:fresh --seed can't seed a table into MySQL

I just started learning PHP and Laravel 7 with no prior experience. I think Artisan and I are having some trust issues and I don't know why. :) I reached the seeding step and I'm facing an error that I need your expertise to solve in an easy way.…
kha
  • 7
  • 1
  • 1
  • 3
0
votes
0 answers

How do I seed laravel from sql file(contains links to other sql files)

I have two files employees.sql SELECT 'CREATING DATABASE STRUCTURE' as 'INFO'; DROP TABLE IF EXISTS employees_test; CREATE TABLE employees_test ( emp_no INT NOT NULL, first_name VARCHAR(14) NOT NULL, last_name …
Serhii Shliakhov
  • 2,631
  • 3
  • 19
  • 37
0
votes
1 answer

Laravel database FooSeeder still seeding when commented in the call function of DatabaseSeeder.php

All my seeders files are in database/seeds/: $ ls -1 database/seeds/ BarSeeder.php DatabaseSeeder.php FooSeeder.php UserSeeder.php Even if I comment FooSeeder in the call function of database/seeds/DatabaseSeeder.php, it's still…
DevonDahon
  • 7,460
  • 6
  • 69
  • 114
0
votes
1 answer

I cannot seed the data on my first ASP.NET CORE APP

Im learning ASP.NET CORE , and also Im building my first web project. The project is about renting cars. I will show what've done so far to the project and what is my problem. I implement the data models: 1 2.I write the CarCategory properties and…
0
votes
1 answer

Need help on finding appropriate Laravel seeder for German cities and states

I need help on finding right package, or database seeder for German cities, that have appropriate states linked to them and also with postal code values. Does someone know any, or is there some other way to implement that in my Laravel application?…
mrmar
  • 1,407
  • 3
  • 11
  • 26
0
votes
2 answers

Create seeder for a table with two foreignkeys

I was trying to create a seeder for Products table with two foreign keys (category_id and sub_category_id for categories and sub_categories tables respectively). Category::all()->each(function ($category) { SubCategory::all()->each(function…
MDB
  • 339
  • 4
  • 19
0
votes
2 answers

Laravel Seeding produces 3 times more results than expected

I'm working on a Laravel project and trying to seed the database by using model factories. I have a Banners table and related BannerTranslations model to hold translated values. The project will have 3 languages. I'm trying to seed 5 dummy values…
megavolkan
  • 197
  • 1
  • 2
  • 13
0
votes
1 answer

Laravel Seeder Class and the 3rd item on the array gives Exception

I'm seeding a [skills] table but I'm getting an error in the console and it's so vague. If you check, nothing useful really. Just an exception. The data that I'm trying to seed are string and images so naturally, the column that accepts the image…
Shu Pesmerga
  • 155
  • 1
  • 1
  • 15
0
votes
1 answer

laravel userstamps without disturbing the database seeding

Is there any way to automate the filling of a created_by column without the use of observers, but keeping my controllers clean? I don't want to use observers because when I try to seed my database, errors happen. The way I'm doing now: class…
Vitor Mattos
  • 56
  • 2
  • 8
0
votes
1 answer

Laravel 6.12 seeding factory don't see states

As usual the project needed seeds. And as usual I create a UserFactory, set it up, added to DatabasesSeeder and caught a error. InvalidArgumentException : Unable to locate [models] state for [App\Models\User] but the default factory (I mean…
Tig
  • 38
  • 4
0
votes
2 answers

Problem with Laravel Seeding with Factories

I have a problem with Laravel Seeding with Factories : I have three tables users, products and products_categories. I have a ProductFactory class which is called in ProductSeeder. Finally, I have my DatabaseSeeder who executes all seeders. When I…
0
votes
2 answers

How can I run a seeder from a cronjob in Laravel?

I need to run a seeder what it takes a time to execute (like five minutes). But i set it in my crontab as: 0 0 * * * php /var/www/.../api/artisan db:seed --class=ApiPlayerStatisticsSeeder And it does not run by itself. Looks like it need an user to…
Simón Farias
  • 732
  • 2
  • 8
  • 21