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

Laravel Polymorphic Database Seeder Factory

How can I create a database seeder factory for the following configuration? User // create_users_table.php Schema::create('users', function (Blueprint $table) { $table->increments('id'); ... } // User.php public function notes() { …
HyperionX
  • 1,332
  • 2
  • 17
  • 31
6
votes
1 answer

Laravel Factory ->each() add iterator

I have a factory: factory(Survey::class, 3)->create()->each(function ($survey) { factory(Question::class, 1) ->create() ->each(function ($question) { factory(Option::class,…
Denys Siebov
  • 198
  • 4
  • 16
6
votes
3 answers

Laravel 5 Seeding

I am following the tutorial in "Laravel 5 Essentials." When I try to seed my database with the command php artisan db:seed I receive the error [ReflectionException] Class BreedsTableSeeder does not exist The code for BreedsTableSeeder is…
jstein
  • 633
  • 1
  • 8
  • 14
5
votes
1 answer

Updating the data of an existing column and copying data with migrations

Is it possible to add a new column, copy some data to this new column and update the data in another column with a Laravel migration? I have a table something like this; id item price 1 ItemOne 100.00 2 ItemTwo 200.00 Now what I need…
5
votes
2 answers

Laravel Factories and Seeding: Static array of arrays

For some of my tables, I'd like to insert a fixed amount of rows with specific data. This is my categories factory: $factory->define(Category::class, function (Faker $faker) { return [ [ 'name' => 'Politics', …
good_afternoon
  • 1,529
  • 1
  • 12
  • 41
5
votes
1 answer

Laravel 5.4 Seeds not seeding all tables

I'm seeding in Laravel 5.4 but it only seeds one table, the others are not seeded. The seeders were created using the command: php artisan make:seeder seederName
5
votes
0 answers

How do I optimise laravel seeds beyond mass insertion for faster seeding/

So I am developing a laravel application and I am trying to get my seeds optimised so that they run faster. http://bensmith.io/speeding-up-laravel-seeders This guide helped a ton. According to this, I should minimise the number of SQL queries by…
Rohan
  • 13,308
  • 21
  • 81
  • 154
5
votes
1 answer

Laravel seeder inserts only a random number of data and then fails

I have this Seeder function. It takes all inserted users and then, for each of them, generate a random number (from 0 to 12) of profile picture. public function run() { foreach (App\User::all() as $user) { echo "Generating profile pic…
David
  • 4,785
  • 7
  • 39
  • 63
4
votes
1 answer

Laravel factory Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given

I get this error when trying to run a factory in laravel 8. I've looked though several posts about this error but they all seem to come from saving/creating directly incorrectly. Not using a factory. So I am not sure why the factory isn't saving it…
user16092281
4
votes
2 answers

Target class [Database\Seeders\UsersTableSeeder] does not exist

I am getting the error "Target class [Database\Seeders\UsersTableSeeder] does not exist" and I cannot figure out why. I have already tried solutions posted to similar issues and none of them worked for me. Here is my composer.json autoload/classmap…
Ken
  • 693
  • 1
  • 8
  • 14
4
votes
1 answer

DB Seeding Error: Column not found: 1054 Unknown column 'client_id' in 'field list'

My top-level table is clients and the table users belongs to clients. This is the error I get when trying to seed the user's table. Illuminate\Database\QueryException : SQLSTATE[42S22]: Column not found: 1054 Unknown column 'client_id' in 'field…
Colin M
  • 139
  • 1
  • 12
4
votes
1 answer

Factory Seed pivot table in Laravel with many-to-many relationships

I'm completely new to factories and seeds, and until now I've been manually adding data to each project in Sequel Pro or Workbench an. I'm fairly new to Laravel in general, but I think I've got the models setup correctly. I've got a recipe that can…
n8udd
  • 657
  • 1
  • 9
  • 30
4
votes
1 answer

Field doesn't have default value upon factory seeding against a one to many relationship

I have one speciality that can have many procedures. I am wanting to seed both the speciality with many procedures. Models: Speciality class Speciality extends Model { protected $fillable = ['name']; public function procedures() { …
user860511
4
votes
1 answer

Laravel 5.2 database seeds in packages?

I'm creating a package and want to have db seeds in it. All those seeds will do is add some new rows to a table that already exists. I'd also like an "unseed" option to remove those rows when the package is uninstalled. I'm unsure how to go about…
Warz
  • 333
  • 3
  • 11
4
votes
4 answers

Faker having end date greater than start date

We are using Faker for our Laravel app. For a table we have 2 columns, start date and end date. How to call faker in a way that the end date will always be greater than start date
Kannan Ramamoorthy
  • 3,980
  • 9
  • 45
  • 63
1 2
3
21 22