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

Getting a Parse error: syntax error, unexpected '$faker' error when seeding

I am getting the following error when trying to seed in Laravel 5.4 [Symfony\Component\Debug\Exception\FatalThrowableError] Parse error: syntax error, unexpected '$faker' (T_VARIABLE), expecting function…
AltBrian
  • 2,392
  • 9
  • 29
  • 58
0
votes
1 answer

Laravel: error while seeding database from an url

I'm trying to seed my database table. I'm getting this following error. public function run() { // $json = file_get_contents('http://www.ottawacityjobs.ca/en/data/'); $emplois = json_decode($json); foreach ($emplois as $item) { …
Papouche Guinslyzinho
  • 5,277
  • 14
  • 58
  • 101
0
votes
2 answers

Laravel: error while seeding the database (Array to string conversion)

I'm trying to seed the database. Following the User example I create a Emploi (Job) seeder but I'm having an error. seeding file $factory->define(App\Emploi::class, function (Faker\Generator $faker) { return [ 'JOBURL' => $faker->name, …
Papouche Guinslyzinho
  • 5,277
  • 14
  • 58
  • 101
0
votes
1 answer

Model and Seeding Difference in Laravel

I am confused what's the difference of seeding and by using model in laravel which both saving data into the database. Why not just use Model? or Why not Seed? When and why they used? Answers will be appreciated.
Mr. J
  • 245
  • 6
  • 24
0
votes
3 answers

Table not found when seeding

Below are my migrations The users table have a relation with the customer table In user: $table->integer('customer_id')->unsigned(); $table->foreign('customer_id')->references('id')->on('customers'); When i call php artisan migrate:refresh --seed,…
yooouuri
  • 2,578
  • 10
  • 32
  • 54
0
votes
1 answer

Creating Seeder with foreign key field Laravel 5.3

I'm trying to create a seeder for my table addresses but one field of my table, is a foreign key, This Fk references a user id of my table users. My Seeder Class: class AddressesSeeder extends Seeder { public function run() { $faker…
Daniel Cintra
  • 71
  • 1
  • 1
  • 9
0
votes
1 answer

Lumen database seeding error

I'm learning back-end development with Laravel's Lumen framework, and I am writing database seeding class following Laravel's documentation. Below are the codes: Model app\Photo.php namespace App; use Illuminate\Database\Eloquent\Model; class…
Nhan
  • 3,595
  • 6
  • 30
  • 38
0
votes
1 answer

Laravel 5.3 Notifications Seeder

What I am trying to acheive - is create a database seeder for Laravel Notifications. As far as i am using database to store my notifications, there should be a way to achieve it. I am creating Factory model : $user->notify(new…
Taki
  • 68
  • 8
0
votes
0 answers

Laravel Seeder with .sql file

I want to insert 30K records to my table. I have the .sql file for the same table and want to do with laravel seeder. DB::disableQueryLog(); DB::unprepared(file_get_contents('database/seeds/sql/inv_diagnostic_fe.sql')); Its only inserting 338 rows.…
Jishad
  • 2,876
  • 8
  • 33
  • 62
0
votes
1 answer

How to do incremental seed in Laravel?

I'm asked to add a new role to existing live system developed in Laravel 5.2. We already have RoleSeeder. But if I add a new entry to RoleSeeder, I can't just run php artisan db:seed --class=RoleSeeder since it will try to recreate the other…
DarkVenture
  • 65
  • 10
0
votes
4 answers

Laravel 5.3 Seeder - undefined method table?

I'm trying to learn Laravel, but it seems the documentation is written with faulty examples... I want to create a table migration, run it, and seed it with some content. First: php artisan make:migration create_projects_and_tasks_tables With the…
user3430483
  • 57
  • 10
0
votes
1 answer

how to have html codes in mysql or json in laravel using seeder

I'm trying to build a website builder, where I want to store HTML structure of the plugins/portlets manually, where user can have the structure and whatever editable data they are inserting, gets stored into database using serialize in the…
Nitish Kumar
  • 6,054
  • 21
  • 82
  • 148
0
votes
2 answers

Laravel 5.2: Update .env file with user input & run migrations through code

I'm creating an app and I am planning to give users a one click installation feature. The idea is to show a form when the application is first launched, sort of Installation / Configuration screen where user will type Database details (hostname, db…
Jazzbot
  • 385
  • 1
  • 4
  • 18
0
votes
1 answer

Seeding Table with auto increment field as zero

I created a seeder for a table, and the first record I am trying to insert has ID=0. The ID is an auto increment field, and when it gets inserted, I check its ID and it is 1 instead of 0, so the seeder breaks in the next line, when I try to create a…
Miguel Mesquita Alfaiate
  • 2,851
  • 5
  • 30
  • 56
0
votes
0 answers

Lumen (Laravel): Unable to run php artisan db:seed due to missing class

I'm using Lumen and working through a 12 month-old tutorial. While attempting to run php artisan db:seed, I'm getting the following error: [Symfony\Component\Debug\Exception\FatalThrowableError] Class 'App\Models\Quote' not found However…
sparecycle
  • 2,038
  • 5
  • 31
  • 58