6

I have a database seed file:

class ContactTableSeeder extends Seeder {
    public function run()
    {
        $contacts = array(
            array(
                'first_name'        => 'Test',
                'last_name'         => 'Contact',
                'email'             => 'test.contact@emai.com',
                'telephone_number'  => '0111345685',
                'address'           => 'Address',
                'city'              => 'City',
                'postcode'          => 'postcode',
                'position'          => 'Director',
                'account_id'        => 1
           )
        );

        foreach ($contacts as $contact) {
            Contact::create($contact);
        }
    }
}

When I run php artisan migrate:refresh --seed it seeds the database and creates the relevant record in the contacts table, except that it does not fill the fields with any of the information in the seed array. I am using the exact same syntax for other tables and they work fine, and I've also checked each field thoroughly to make sure they match the database fields but no matter what I do it will not seed correctly.

Does anyone have any ideas?

Gareth Daine
  • 4,016
  • 5
  • 40
  • 67
  • Have you set $fillable or $guarded property in your model? If not, then Laravel won't let you fill the fields with ::create() method due to mass assignment. Check here - http://laravel.com/docs/5.1/eloquent#mass-assignment – naneri Nov 15 '15 at 07:16

4 Answers4

14

I had this same problem but none of the above solutions worked for me. It turned out to be due to having a construct function in my model! After I removed this it worked fine!

public function __construct()
{
    parent::__construct();
}

EDIT: After further reading on this I discovered the issue is due to the fact that if you are going to include a constructor in your model must accept the attributes parameter and pass it to the parent. If you do this then the constructor does not break the DB seeding (and probably other things). I hope this saves someone else a headache.

public function __construct($attributes = array())
{
    parent::__construct($attributes);
}
naw103
  • 1,843
  • 1
  • 15
  • 14
1

Turns out the issue was to do with relationships in my models.

For future visitors to this question: make sure to check all the functions in your models that define hasOne/hasMany/etc relationships. Read though the Eloquent docs for more.

DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290
Gareth Daine
  • 4,016
  • 5
  • 40
  • 67
  • 1
    Can you be more explanatory about what was finally the problem with your relationships? – giannis christofakis Nov 04 '13 at 11:14
  • I can't quite remember sorry. I think it may have been because I had not setup the Contact model. – Gareth Daine Nov 05 '13 at 14:31
  • I just had this problem. In my case I had added this to the User model: `public function user() { return $this->hasOne('Manager', 'userid'); }` But it should have been a `manager()` function. You just need to make sure the model relationships are correct. – DisgruntledGoat Jul 31 '14 at 15:36
0

Have you tried to replace the following lines:

foreach ($contacts as $contact) {
   Contact::create($contact);
}

with

DB::table('contact')->insert($contacts);

assuming your table name is contact. And also, make sure you have line like this

$this->call('ContactTableSeeder');

in your DatabaseSeeder class.

giannis christofakis
  • 8,201
  • 4
  • 54
  • 65
vlad
  • 546
  • 6
  • 5
0

Do you have $this->call("ContactTableSeeder") in your DatabaseSeeder class' run() function?

If you have ContactTableSeeder in it's own file, is the file named ContactTableSeeder.php exactly? If not it would fail to load according to the PSR-0 Standard.

These are my first thoughts.

Mark Smith
  • 621
  • 6
  • 8
  • I just edited one of my seed files to match your syntax and it's working fine, although mine is called `TenantTableSeeder`. Have you tried `composer update` to get the latest version of Laravel 4? – Mark Smith May 11 '13 at 14:17
  • I am calling $this->call('ContactTableSeeder') in my DatabaseSeeder class run() method and the filename is correct. I have also ran composer update with no errors. The only table that works with that syntax is my users table. – Gareth Daine May 11 '13 at 14:25
  • The problem isn't the calling of the seed or even inserting the records, it does insert them, they are simply empty. – Gareth Daine May 11 '13 at 14:27
  • How about pasting the source of your DatabaseSeeder.php file here: http://paste.laravel.com – Mark Smith May 11 '13 at 14:27
  • Also, your Contact.php and ContactTableSeeder.php file? – Mark Smith May 11 '13 at 14:28
  • I should note, that this is happening on all seeds except for the UserTableSeeder.php which is exactly the same syntax. – Gareth Daine May 11 '13 at 14:32
  • I hate to say it but it all looks good to me. Is there something different about your User model than the others? – Mark Smith May 11 '13 at 14:38
  • No but I've just discovered that my greetings table seeder is working fine and that is using the same syntax too. Here's the model http://paste.laravel.com/rgC – Gareth Daine May 11 '13 at 14:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29774/discussion-between-mark-smith-and-gaz) – Mark Smith May 11 '13 at 14:47