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
17
votes
5 answers

How to implement your own Faker provider in Laravel

I want to create a custom provider for Faker in Laravel (e.g. one for a random building name). Where do I store the custom provider in my application and how do I use it?
carte
  • 1,033
  • 2
  • 13
  • 29
16
votes
5 answers

Laravel Factory: Manual Increment of Column

For the following factory definition, the column order needs to be sequential. There is already a column id that is auto-incremented. The first row's order should start at 1 and each additional row's order should be the next number (1,2,3,…
bnahin
  • 796
  • 1
  • 7
  • 20
16
votes
5 answers

Laravel: Seeding multiple unique columns with Faker

Introduction What up folks, I got a question about model factories and multiple unique columns: Background I have a model named Image. This model has language support stored in a separate model, ImageText. ImageText has an image_id column, a…
Rkey
  • 690
  • 2
  • 8
  • 25
16
votes
2 answers

Laravel seeding results in Null timestamp

So i had a seeder for Languages Table (LanguageTableSeeder) as follows: DB::table('languages')->insert([ 'name' => 'English', 'flag' => '', 'abbr' => 'en', 'script' => 'Latn', 'native' => 'English', …
Jhivan
  • 195
  • 1
  • 1
  • 8
16
votes
3 answers

Laravel seed database from existing database

i have now new structure of my database, but i need to import the old data in the new format. For that reason i want to use the Laravel seeder, but i need somehow to connect to the old database and make select queries and to tell the seeder how to…
Vince Carter
  • 907
  • 3
  • 14
  • 41
14
votes
4 answers

Laravel. Disable observer methods if the database is seeding

I have an observer for my User model. Inside my observer->created event i have some code. public function created(User $user) { sendEmail(); } So, the idea is, when a user is created, the system will send for the user email notification that…
12
votes
4 answers

Seed a pivot table using factories in Laravel

I'm new to Laravel and I'm looking for a good way to seed a pivot table using factories. I don't want to use plain seeders. I'll show you the case: I have three tables (users, skills, and user_skill). users user_skill …
alexhoma
  • 342
  • 1
  • 7
  • 19
12
votes
6 answers

How to run laravel migration and DB seeder except one

I am having many migration and seeder files to run, Although I will need to run all files but currently I need to skip one migration and seeder. How could I skip one file from laravel migration and db seeder command. I do not want to delete files…
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
12
votes
4 answers

A four digit year could not be found Data missing

I'm trying to seed using factories in Laravel 5.2 My code dies in the User factory: $factory->define(App\User::class, function (Faker\Generator $faker) { $countries = Countries::all()->pluck('id')->toArray(); return [ 'name' => $faker->name, …
Juliatzin
  • 18,455
  • 40
  • 166
  • 325
11
votes
4 answers

Laravel faker generate gender from name

What is the optimum way of generating gender using faker, having generated a name so that the gender matches the name return [ 'name' => $faker->name, 'email' => $faker->safeEmail, 'username' => $faker->userName, 'phone' =>…
Morris Mukiri
  • 113
  • 1
  • 2
  • 10
10
votes
2 answers

In Laravel Factories, what is the difference from "state" and "defineAs"?

What is the difference, in Laravel's Factories, from using the method state and the method defineAs? Is state just an extension of the base method and defineAs a whole different definition? Could you present some examples to make it easier to…
Johny Serpa
  • 155
  • 1
  • 2
  • 16
10
votes
2 answers

What is database seeding in Laravel?

I use Laravel framework and I have recently been informed that there is something named database seeding which produces a fake dataset for our tests. Is my understanding correct? Well that's pretty much odd. How it works? How it knows which type of…
stack
  • 10,280
  • 19
  • 65
  • 117
9
votes
4 answers

delete test data in laravel migration script

I am trying to clean the test data from my production tables. In simple environment I can write a script to clean the test data but I wonder how can I do the same in laravel migration script I have a test user on production and want to clean all…
user269867
  • 3,266
  • 9
  • 45
  • 65
8
votes
3 answers

Using PHP Faker in Laravel to generate "unique with" entry when seeding a database using a factory

So Similar to the unique with validation rule (See: https://github.com/felixkiss/uniquewith-validator), I want to know how to generate a entry, where one column is unique with another one. I want to seed my database as follows. Example: There are 12…
J. Robinson
  • 931
  • 4
  • 17
  • 45
7
votes
3 answers

Calling Laravel Seeder inside folder

I've upgraded to Laravel 8, but my custom seeder in a subdirectory is not working. The file is at database/seeders/tests/TestSeeder.php. When I run the command php artisan db:seed --class="TestSeeder" it return an error message: Target class…
Kenneth
  • 2,813
  • 3
  • 22
  • 46
1
2
3
21 22