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

Saving static content into the Laravel public folder

I am having trouble storing images in the 'public' folder of the Laravel framework (which I believe is the static content folder? Please correct me if I am wrong). I am running a seed using faker which generates images and stores the images URL's in…
James
  • 2,800
  • 7
  • 45
  • 81
0
votes
2 answers

How to properly handle database data changes with laravel migrations and seeds

I have my migration files, which creates the initial schema for the database, and, I have my seed files, which populates the initial schema, with random data, and set types. My understanding of the migrations and seeds is that, whenever a new team…
lebobbi
  • 2,147
  • 17
  • 20
0
votes
3 answers

Laravel Carbon dates not getting modified in Seeder

In a Laravel Seeder, I am trying to take a Faker generated date, convert it to Carbon and then grab two dates that are just a few hours apart to represent a 4 hour session. For example, Start Date: 03/13/2016 12:00PMEnd Date: 03/13/2016…
jdcarg
  • 125
  • 1
  • 1
  • 6
0
votes
1 answer

Faker for Laravel Seeder

I have a table users in a one-many relationship with a table called videos. I want to seed the two table without loosing data integrity. This is what I did below: $factory->define(App\User::class, function (Faker\Generator $faker) { return [ …
George
  • 3,757
  • 9
  • 51
  • 86
0
votes
1 answer

Laravel 5.1 Database Seeding not working

I have got some problem with Seeding. I create new seed file: php artisan make:seeder RoleUserSeeder i run method i added: DB::table('role_user')->insert([ 'role_id' => 1, 'user_id' => 2, ]); and then try: php artisan optimize Generating…
Pionas
  • 346
  • 1
  • 4
  • 15
0
votes
2 answers

Seeding table using model factory not saving

I have been trying to get seeding set up with the first table on my site using the model factory. The seeds that I entered manually work, but I wanted to make sure I could seed using the factory, as well, for future tests and all that jazz. The…
llhilton
  • 170
  • 5
  • 15
0
votes
1 answer

Seeding a table fails in laravel 5.1

I'm new to Laravel, I'm trying to seed a table, and artisan always returns code 255. Here is my code
Juliatzin
  • 18,455
  • 40
  • 166
  • 325
0
votes
1 answer

Finding and storing address of categories location in hierarchy in pivot table

First of all I am using Laravel 5.1 (PHP framework) and I am running PHP 5.6.4 and the issue I am faced with is really to do with recursion. My seed file at the moment is(with a poor excuse for a recursive function):
Alex Winter
  • 37
  • 1
  • 1
  • 7
0
votes
1 answer

Getting ReflectionException while using a Package

I am using a Package for roles and permissions management in my laravel 5.1 application and getting error while trying to create roles by using following code.
Hassan Saqib
  • 2,597
  • 7
  • 28
  • 51
0
votes
2 answers

Laravel DatabaseSeeding

I'm trying to learn about laravel. I'm not busy with seeding put I keep getting the following error: [ReflectionException] Class UserTableSeeder does not exist What have I done: User class use Illuminate\Auth\Authenticatable; use…
Graham
  • 1,850
  • 4
  • 21
  • 41
0
votes
1 answer

artisan db:seed does not work and does not print an error

My seeder Class code is as follows: delete(); $cats = ['math',…
Hassan Saqib
  • 2,597
  • 7
  • 28
  • 51
0
votes
1 answer

Seeding database - [ErrorException] Trying to get property of non-object

I am using "zizaco/confide": "~4.0@dev" and "zizaco/entrust": "1.2.*@dev". I have set everything up as described in the two tutorials(confide migrations). Furthermore, I have created the following models: User:
Carol.Kar
  • 4,581
  • 36
  • 131
  • 264
-1
votes
1 answer

How to clear a specific table content then seed it

I couldn't find something that do it in one line but... After a search I found that works perfectly with me but if any have better solution please write it below first refresh it php artisan migrate:refresh…
Tomatoes
  • 39
  • 8
-1
votes
1 answer

How to do multi-level seeding in by using Eloquent Relationships in Laravel?

Here is the table stucture Table A Table B has foreign key a_id Table C has foreign key a_id and b_id I am trying to Seed these three tables with Eloquent Relationships Here is my code structure class A extends Model { use HasFactory; …
Ratul
  • 43
  • 2
  • 8