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
3
votes
3 answers

laravel model factory seed generate from existing

I am trying to seed a table with a foreign key and I'm stuck on how I would tell the model to randomly pull values from what already exists. ModelFactory $factory->define(App\Vendor::class, function(Faker\Generator $faker) { return [ …
jon3laze
  • 3,188
  • 6
  • 36
  • 69
3
votes
0 answers

Laravel 5 - Not seeding a table when using php artisan db:seed, but works fine when php artisan db:seed --class is used

I entered php artisan db:seed but it skip seeding CountriesRetailerSeeder. It seeds when I do it manually. php artisan db:seed --class=CountriesRetailerSeeder How will I be able to run it in php artisan db:seed along with the other seeders ? here's…
KennethC
  • 746
  • 2
  • 10
  • 27
3
votes
2 answers

php artisan db:seed is not working Laravel 5

I have read other answers but nothing solved my issue. When I run php artisan db:seed nothing happens, even no error is thrown.This is my DatabaseSeeder class
StealthTrails
  • 2,281
  • 8
  • 43
  • 67
3
votes
2 answers

Naming Conflict in Laravel 5 Database Seeding

i am running into a naming conflict when running my database seeds (along with other console commands). all of my models are stored in the app/Models directory, and are in the App\Models namespace. there is a base model in this directory called…
Andrew Brown
  • 5,330
  • 3
  • 22
  • 39
3
votes
2 answers

Laravel 5 Seeding

I'm following the docs to seed the users table, which shows User::create being used class UserTableSeeder extends Seeder { public function run() { DB::table('users')->delete(); User::create([ 'username' => 'test', …
mtpultz
  • 17,267
  • 22
  • 122
  • 201
2
votes
2 answers

function inside a seeder giving Call to undefined function Termwind\ValueObjects\mb_strimwidth()

i have a project where i have a 'habits' table, and that table needs a seeder to give it data, simply put, its a seeder with raw data, and i need to cook it, in other terms i need to process it like so... public function run() { $Habits = [ …
Evinn
  • 33
  • 4
2
votes
4 answers

How to send a value as parameter to the Factory Class

I need to run a Factory 50 times, so inside the DatabseSeeder: public function run() { for($i=1;$i<=50;$i++){ (new CategoryQuestionFactory($i))->create(); } } So as you can see, I tried passing a variable called $i as parameter to…
Pouya
  • 114
  • 1
  • 8
  • 36
2
votes
2 answers

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ahimalayan.localizations' doesn't exist

I'm not a Laravel developer, but I bought a website from CodeCanyon, and this is the error I'm getting. SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ahimalayan.localizations' doesn't exist (SQL: select * from localizations where ip =…
2
votes
2 answers

laravel 8 seeding , SQLSTATE[23000]: Integrity constraint violation:

I have undegraded my project from Laravel 7 to 8. but the problem is when i run the seeding command php artisan db:seed it shows an error SQLSTATE[23000]: Integrity constraint violation:. it's because of it seed the previous seeding data which I run…
Muqadar Ali
  • 57
  • 2
  • 11
2
votes
0 answers

Model events not firing in seeder

I am creating custom unique ids for some of my models. To this end I have created a trait that adds a hook to the creating event on the models that uses it, to generate and set a unique Id. When I create a model from tinker this works fine. But my…
kaan_a
  • 3,503
  • 1
  • 28
  • 52
2
votes
3 answers

Laravel 8 Database Factories

I want to get the override attributes in factory which are defined in seeder at the time of using factories. For example, in Laravel 7 it was possible to get them as the third parameter $factory->define(Menu::class, function (Faker $faker, $params)…
Mira Thakkar
  • 339
  • 1
  • 6
  • 19
2
votes
1 answer

Laravel Factory error Allowed memory size of 536870912 bytes exhausted (tried to allocate 262144 bytes)

I want to generate dummy data using factory with seeder so it will give me the error. when I run this command given below: php artisan db:seed so here it's the error. PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to…
Mubashir
  • 75
  • 1
  • 3
  • 9
2
votes
1 answer

Laravel: Seeding Database table with relationship When the table has two foreign key

I am using laravel factory method for populating my database table. I have three models and their relationship with each other are given below: **User model** class User extends Authenticatable { public function books() { return…
Milan Budhathoki
  • 61
  • 2
  • 3
  • 7
2
votes
2 answers

Problem with seeding data in pivot table in Laravel

I have a problem while attempting to seed my pivot table in database in Laravel. I get error Base table or view not found: 1146 Table 'user_cuisines' doesn't exist And my table name is actually user_cuisine, so it is like for some reason laravel…
rose
  • 447
  • 5
  • 18
2
votes
3 answers

Problem with seeding users to database in Laravel

I am trying to seed users in database but I get error saying Symfony\Component\Debug\Exception\FatalThrowableError : Call to a member function random() on bool I have users table and genders table with gender_id in users table that points to Man…
mrmar
  • 1,407
  • 3
  • 11
  • 26