Search code examples
laravelseeding

Laravel: seeding tables other than Users


UPDATE: I am going to include my full file replacing the partial view I had. The seeder for the User table works, but the one for the Groups table does not. I do have those tables produced by Sentry but I only created a Model for Groups that has nothing in it other than the declaration of the class. Don't know what else to include.

<?php

class DatabaseSeeder extends Seeder {

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

        //User::create(array('email' => '[email protected]'));

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

        $this->command->info('User table seeded!');
    }
    }


    class UserTableSeeder extends Seeder {

        public function run()
        {
             User::create(array(
                       'username' => 'alvaro',
                       'permissions' =>'{"user":1}'
                       ));

            $this->command->info('User table seeded!');
        }
    }   


    class GroupTableSeeder extends Seeder {

        public function run()
        {
             Group::create(array(
                       'name' => 'usuario',
                       'permissions' =>'{"user":1}'
                       ));

            $this->command->info('Group table seeded!');
        }
    }   

But actually, the one I want is the Groups tables (I am on Sentry). Yes, I have created the Model for Group, as Group.php but I don't know how to define its contents. Sometimes I have seen on other occasions that it suffices with just defining the class, but here I dont know, it doesn't work that easily.

Just doing something like

class GroupTableSeeder extends Seeder

will not work as it says that such class does not exist.


Solution

  • The only thing I needed to do was to create a separate file having that name GroupTableSeeder.php

    and include the code in there. For some reason, while UserTableSeeder can be inside a file called DatabaseSeeder and it works, it does not work for other tables.

    class GroupTableSeeder extends Seeder {
    
            public function run()
            {
                 Group::create(array(
                           'name' => 'basicuser',
                           'permissions' =>'{"user.create" :-1,"user.delete" :-1,"user.view":1,"user.update":-1,"post.create":1}'
                           ));
    
                $this->command->info('Group table seeded!');
            }
        }