Laravel种子填充无法填入字段。

6

我有一个数据库种子文件:

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?


你在模型中设置了$fillable或$guarded属性吗?如果没有,Laravel将不允许你使用::create()方法填充字段,因为这是一种批量赋值。请查看此处 - http://laravel.com/docs/5.1/eloquent#mass-assignment - naneri
4个回答

15

我遇到了同样的问题,但以上解决方案都不起作用。后来发现这是由于我的模型中有一个构造函数!在我将其移除后,它就正常工作了!

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

编辑: 进一步阅读后,我发现这个问题是由于如果你要在模型中包含构造函数,必须接受属性参数并将其传递给父类。如果你这样做,那么构造函数就不会破坏数据库的种子数据(可能还有其他事情)。我希望这能为其他人节省一些麻烦。

public function __construct($attributes = array())
{
    parent::__construct($attributes);
}

1
原来问题出在我的模型关系上。
对于未来访问者:请确保检查定义hasOne/hasMany等关系的模型中的所有函数。阅读Eloquent文档以获取更多信息。

1
你能更详细地解释一下你和关系方面最终出了什么问题吗? - giannis christofakis
抱歉,我记不太清了。我想可能是因为我没有设置联系人模型。 - Gareth Daine
我之前遇到了这个问题。在我的情况下,我已经将以下代码添加到用户模型中:public function user() { return $this->hasOne('Manager', 'userid'); } 但实际上应该是一个 manager() 函数。你只需要确保模型关系正确即可。 - DisgruntledGoat

0

你尝试过替换以下行吗:

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

使用

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

假设您的表名为contact。 并且,请确保您有这样一行代码。
$this->call('ContactTableSeeder');

在你的DatabaseSeeder类中。


我的表名是contacts。我应该能够像其他种子一样使用Eloquent来填充数据库。 - Gareth Daine
当使用DB :: table('contacts').insert($ contacts)时,它会显示未定义函数插入()。我正在使用Laravel 4。 - Gareth Daine
我之前的回答是错误的。正确的代码应该是:DB::table('contact')->insert($contacts); - vlad
好的,你的语法有误,应该是DB :: table('contacts')-> insert($ contacts); 这样就可以了。那行不通,为什么Eloquent不起作用呢? - Gareth Daine
@Gaz - 大规模赋值保护可能会阻止它吗?http://four.laravel.com/docs/eloquent#mass-assignment - Dave James Miller

0

你的 DatabaseSeeder 类的 run() 函数中是否有 $this->call("ContactTableSeeder")

如果你把 ContactTableSeeder 放在了它自己的文件中,那么这个文件名是不是确切地命名为 ContactTableSeeder.php ?否则,根据 PSR-0 标准,它将无法加载。

这是我的第一印象。


我刚刚编辑了我的种子文件以匹配你的语法,它正常工作,尽管我的文件名为TenantTableSeeder。你尝试过使用composer update获取Laravel 4的最新版本吗? - Mark Smith
我在我的DatabaseSeeder类的run()方法中调用了$this->call('ContactTableSeeder'),并且文件名是正确的。我也运行了composer update,没有出现错误。使用该语法工作的唯一表格是我的用户表。 - Gareth Daine
此外,你的 Contact.php 和 ContactTableSeeder.php 文件呢? - Mark Smith
值得注意的是,这种情况发生在所有的种子文件中,除了UserTableSeeder.php,其语法完全相同。 - Gareth Daine
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/29774/discussion-between-mark-smith-and-gaz - Mark Smith
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接