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

Processing Laravel job with many HTTP requests inside, it fails trying by timeout

I have an API REST built using Laravel 5.8 framework. I have to get the data for my app from a public REST API. For this, i have to do a lot of requests. I made a seeder what do this, and it takes 2 minutes for the entire migration data process…
Simón Farias
  • 732
  • 2
  • 8
  • 21
0
votes
1 answer

Laravel How to create a relationship between two Seeders

In my laravel app I have two tables in the database: - education_categories - educations Now, I want to create two Seeders with a relationship between those to. The EductionCategory Seeder: public function run() { …
ST80
  • 3,565
  • 16
  • 64
  • 124
0
votes
3 answers

Laravel eloquent multiple belongsTo seeding

I have three models, User, Category and Article. Article belongs to both a User and a Category where the foreign keys user_id and category_id are not null. Am lost trying to seed the database with fake data via factories accordingly, here's the…
Salim Djerbouh
  • 10,719
  • 6
  • 29
  • 61
0
votes
1 answer

Seeding a MySQL table using Laravel and Carbon for next x number of days

I'm building a feature to build a playlist for next 5 years. In my table, MySQL date is my identifier and has a relation to another table which lists all podcast. My issue is not finding enough guidance with regards to seeding a table, from today…
JJ The Second
  • 77
  • 3
  • 11
0
votes
1 answer

I want to add in database table just name of image, NOT a whole path of image and I want to do with Factory and Seeds

I need to create 5 record in database. I have name, price and path column. I want to add image for column path in database, which should contains name of image NOT whole path using just factory and seeds from Laravel. //PostFactory.php use…
JohnnyJP
  • 7
  • 5
0
votes
1 answer

Laravel 5.8 factory states Unable to locate factory with name [default] [CLASS]

I'm trying to create two different sets of seeders and attempting to use Laravel's factory states to specify different factories for each seeder. Here is an basic example of a BoopieFactory: $factory ->state(App\Models\Boopie::class,…
Matt Larsuma
  • 1,456
  • 4
  • 20
  • 52
0
votes
0 answers

How do I "use" a class in a Laravel seeder file?

I have a Laravel seeder file in which I want to use a Utils class that I created. The Utils class is located at app/Classes/Utils.php, but when I add use App\Classes\Utils as U; at the top of my seeder file and try to call static methods in the…
HartleySan
  • 7,404
  • 14
  • 66
  • 119
0
votes
1 answer

Can not run Seeding on Laravel

Docker 18.09.2 Mysql 5.7 Laravel 5.7 PHP 7.125 I am new at Laravel and I have a problem When I use php artisan db:seed, this error appeared: include(/var/www/laravel_app/vendor/composer/../../database/seeds/AdminsTableSeeder.php): failed to open…
pop
  • 69
  • 1
  • 1
  • 8
0
votes
1 answer

Laravel Factories how to call them from your database seeder file

How to call the factorie files from the database seeder I looked at the laravel docs and was trying around whit this code https://laravel.com/docs/5.7/seeding factory(App\User::class, 50)->create()->each(function ($user) { …
HashtagForgotName
  • 651
  • 1
  • 7
  • 23
0
votes
1 answer

Laravel db:seed does not work without --class parameter

Running php artisan db:seed does not work for some reason on my Laravel 5.6 project. The command runs (quietly) even without a database Does not return any error on the terminal However, when I run php artisan db:seed --class=ClassNameTableSeeder…
Fenn-CS
  • 863
  • 1
  • 13
  • 30
0
votes
1 answer

Laravel seeder loop with recursive table is unable to see inserted data after every increment

I am creating a Laravel factory for creating product models and then using a seeder file to create 100 product factories. The product model has a self referencing column and I know that if there are no product models in the database that this column…
kylerdmoore
  • 93
  • 1
  • 7
0
votes
1 answer

Laravel Factory Can't Save Relation

I use Laravel 5.6, and I have a problem with my seeder. I use this : factory(\App\Models\Merchant::class)->create([ 'email' => 'admin@domain.com', ])->each(function ($m) { $m->stores()->save(factory(\App\Models\Store::class)->create() …
Vincent Decaux
  • 9,857
  • 6
  • 56
  • 84
0
votes
1 answer

Seeding a test database in Laravel using factories and a deterministic seed

I have a number of factories generating models using faker, as recommended in the documentation: $factory->define(App\Member::class, function (Faker $faker) { return [ 'name' => $faker->name() ]; }); I would like to use these…
thodic
  • 2,229
  • 1
  • 19
  • 35
0
votes
1 answer

laravel databse seed pivot table

I've this tables in my Laravel app: Users Books Categories book_category Authors author_book im seeding my table wth this code: factory(App\User::class, 50)->create()->each(function ($user) { $user->books() …
Amin Sh
  • 3
  • 1
0
votes
2 answers

Laravel Translatable factory seeding with dynamic locales

I want to factory seed a translatable model without hardcoding the locales. There are two values that I have to seed, slug which is not translatable, and title which has to be translated to all the languages from the languages table. Here is the…
failedCoder
  • 1,346
  • 1
  • 14
  • 38