Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Seeding Does not Fill in Fields

I have a database seed file:

class ContactTableSeeder extends Seeder {
    public function run()
    {
        $contacts = array(
            array(
                'first_name'        => 'Test',
                'last_name'         => 'Contact',
                'email'             => '[email protected]',
                '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?

like image 370
Gareth Daine Avatar asked May 11 '13 13:05

Gareth Daine


2 Answers

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);
}
like image 69
naw103 Avatar answered Nov 19 '22 20:11

naw103


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.

like image 24
Gareth Daine Avatar answered Nov 19 '22 19:11

Gareth Daine