eddy1992's avatar

db:seed

Hi i am trying to seed my database with fake data but when I did php artisan db:seed it give me an error: syntax error unexpected 'class' (t_class) expecting function (t_function). Dont know why I am getting this error.

My Database seeder.php file

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class DatabaseSeeder extends Seeder {

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();

        $this->call('UserTableSeeder');
    }

    class UserTableSeeder extends Seeder
    {
        public function run()
        {
            DB::table('users')->delete();
            User::create([
                            'name' => 'John Doe',
                            'email' => 'Foo@example.com',
                        ]);
        }
    }
}

I am using laravel 5 in windows. Thanks

0 likes
10 replies
RomainLanz's avatar
Level 14

Hi @eddy1992,

The class UserTableSeeder is inside your other class (DatabaseSeeder). This is not allowed in PHP.

Here's the correct code.

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class DatabaseSeeder extends Seeder {

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();

        $this->call('UserTableSeeder');
    }
}

class UserTableSeeder extends Seeder
{
    public function run()
    {
        DB::table('users')->delete();
        User::create([
            'name' => 'John Doe',
            'email' => 'Foo@example.com',
        ]);
    }
}
1 like
eddy1992's avatar

@RomainLanz I create a new file named UserTableSeeder.php in seeds folder and the pasted your code

<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\User;

class UserTableSeeder extends Seeder
{
    public function run()
    {
        DB::table('users')->delete();
        User::create([
            'name' => 'John Doe',
            'email' => 'Foo@example.com',
        ]);
    }
}

Then I ran db:seed and nothing happend in localhost/phpmyadmin users table. Correct me if I am wrong db:seed will add fake data in the users table right ?

eddy1992's avatar

@RomainLanz I did php artisan db:seed --class=UserTableSeeder it gave me an error class UserTableSeeder does not exist

bobbybouwmann's avatar

You deleted the table and then try to add something, that doesn't work! You need to truncate the table before you do that!

DB::table('users')->truncate(); // This will remove all records

Now I would advice you to remove all tables and run php artisan migrate again and then php artisan db:seed

If you want to be faster you can do something like this

php artisan migrate --seed
1 like
bobbybouwmann's avatar

If it can't find the class you need to run composer dump-autoload

1 like
eddy1992's avatar

@bobbybouwmann this will add one table entry how will I add like 10 data entry to table ?

Thank you for your reply.

bobbybouwmann's avatar

You can use a for loop to do that

for($i = 0; $i < 10; $i++) 
{
    User::create([
        'name' => 'John Doe ' . $i,
        'email' => 'Foo' .  $i . '@example.com',
    ]);
}

Note: This is a really basic example, but just to get you started ;)

1 like

Please or to participate in this conversation.