Laravel Seeding Does not Fill in Fields
Asked Answered
I

4

6

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?

Intrigante answered 11/5, 2013 at 13:14 Comment(1)
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 - laravel.com/docs/5.1/eloquent#mass-assignmentBaby
I
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.

Intrigante answered 12/5, 2013 at 15:42 Comment(3)
Can you be more explanatory about what was finally the problem with your relationships?Greeneyed
I can't quite remember sorry. I think it may have been because I had not setup the Contact model.Intrigante
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.Breastsummer
S
15

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);
}
Sharpset answered 15/11, 2015 at 5:32 Comment(0)
I
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.

Intrigante answered 12/5, 2013 at 15:42 Comment(3)
Can you be more explanatory about what was finally the problem with your relationships?Greeneyed
I can't quite remember sorry. I think it may have been because I had not setup the Contact model.Intrigante
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.Breastsummer
A
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.

Alfonsoalfonzo answered 11/5, 2013 at 14:1 Comment(5)
My table name is contacts. I should be able to use Eloquent to seed the database, as I am in other seeds.Intrigante
When using DB::table('contacts').insert($contacts); it says call to undefined function insert(). I'm using Laravel 4.Intrigante
I was mistaken in my answer. It should be DB::table('contact')->insert($contacts);Alfonsoalfonzo
OK your syntax was wrong, it should be DB::table('contacts')->insert($contacts); That worked, so why does Eloquent not work.Intrigante
@Gaz - Mass assignment protection may be preventing it? four.laravel.com/docs/eloquent#mass-assignmentGrowth
M
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.

Manteau answered 11/5, 2013 at 14:8 Comment(9)
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?Manteau
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.Intrigante
The problem isn't the calling of the seed or even inserting the records, it does insert them, they are simply empty.Intrigante
How about pasting the source of your DatabaseSeeder.php file here: paste.laravel.comManteau
Also, your Contact.php and ContactTableSeeder.php file?Manteau
I should note, that this is happening on all seeds except for the UserTableSeeder.php which is exactly the same syntax.Intrigante
I hate to say it but it all looks good to me. Is there something different about your User model than the others?Manteau
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 paste.laravel.com/rgCIntrigante
let us continue this discussion in chatManteau

© 2022 - 2024 — McMap. All rights reserved.