用雄辩的意外行为创建条目

时间:2014-11-21 15:06:48

标签: laravel laravel-4 eloquent

我正在尝试为联系人创建条目:

Contact::create([
            'firstname' => $faker->firstName,
            'lastname' => $faker->lastName,
            'email' => $faker->email,
            'location_id' => 6,
            'department_id' => 3
        ]);

在模型Contact.php中:

protected $fillable = [
    'firstname',
    'lastname',
    'location_id',
    'department_id',
    'email'
];

迁移代码:

Schema::create('contacts', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('firstname');
        $table->string('lastname');
        $table->integer('location_id')->nullable();
        $table->integer('department_id')->nullable();
        $table->string('email')->unique();
        $table->timestamps();
    });

如果我尝试使用firstname,lastname和email创建它,它可以正常工作。但由于某种原因,它不能处理填写的location_id和department_id字段。该条目不会出现在数据库中,但没有错误消息。我做错了什么?

1 个答案:

答案 0 :(得分:-1)

我们试试这个

// app/models/Location.php
class Location extends Eloquent { }

// app/models/Department.php
class Department extends Eloquent { }

和迁移到更新

Schema::create('contacts', function(Blueprint $table)
{
    $table->increments('id');
    $table->string('firstname');
    $table->string('lastname');
    $table->integer('location_id')->unsigned();
    $table->integer('department_id')->unsigned();
    $table->string('email')->unique();
    $table->timestamps();
});