Laravel 5忽略:: create方法中的字段

时间:2015-08-14 14:51:34

标签: php laravel-5 eloquent

有人可以从下面的来源看到为location_id插入NULL的原因吗?

$locationObject = Location::find($location);
if (\ResponseHelper::isValid($locationObject)) {
    print_r("location_id: " . $locationObject->id);

    $recipientObject = Recipient::create(
        [
            'firstname'             => $recipientArray['firstname'],
            'lastname'              => $recipientArray['lastname'],
            'dob'                   => $recipientArray['dob'],
            'last_sent'             => null,
            'last_visit'            => null,
            'source'                => $recipientArray['source'],
            'last_attendance'       => null,
            'last_attendance_class' => '',
            'last_interaction'      => null,
            'last_interaction_user' => '',
            'location_id'           => $locationObject->id,
        ]
    );

    print_r($recipientObject);

应注意$locationObject有效且print_r的结果为:location_id: 20

在PostMan中,打印出对象的结果是:

    [attributes:protected] => Array
    (
        [firstname] => River
        [lastname] => Tam
        [dob] => 1993-04-04
        [last_sent] => 
        [last_visit] => 
        [source] => f151f36183bbe9012a53c4e367ff5a91
        [last_attendance] => 
        [last_attendance_class] => 
        [last_interaction] => 
        [last_interaction_user] => 
        [location_id] => 
        [updated_at] => 2015-08-14 16:55:58
        [created_at] => 2015-08-14 16:55:58
        [id] => 417
    )

在模型中

protected $fillable = [
    'id',
    'firstname',
    'lastname',
    'dob',
    'last_sent',
    'last_visit',
    'source',
    'last_attendance',
    'last_attendance_class',
    'last_interaction',
    'last_interaction_user',
    'location_id',
];

产生的数据库记录

| 392 | 2015-08-14 15:16:43 | 2015-08-14 15:16:43 | NULL      | River     | Tam       | 1993-04-04          | NULL       | NULL       | f151f36183bbe9012a53c4e367ff5a91 | NULL            |                       | NULL             |                       |        NULL |

(这只是为了示例目的,实际的行没有显示,因为我有数千,但结果是相同的,所有location_id的插入都是NULL,所以为此目的这个'demo'就足够了 - location_id就在最后一栏。)

修改 我还应该注意,我刚尝试过:location_id => 20这也是不行的。

编辑2 模式

+-----------------------+------------------+------+-----+---------------------+----------------+
| Field                 | Type             | Null | Key | Default             | Extra          |
+-----------------------+------------------+------+-----+---------------------+----------------+
| id                    | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| created_at            | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| updated_at            | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| last_sent             | varchar(255)     | YES  |     | NULL                |                |
| firstname             | varchar(255)     | YES  |     | NULL                |                |
| lastname              | varchar(255)     | YES  |     | NULL                |                |
| dob                   | varchar(255)     | YES  |     | NULL                |                |
| deleted_at            | timestamp        | YES  |     | NULL                |                |
| last_visit            | datetime         | YES  |     | NULL                |                |
| source                | varchar(255)     | NO   |     | NULL                |                |
| last_attendance       | datetime         | YES  |     | NULL                |                |
| last_attendance_class | varchar(255)     | YES  |     | NULL                |                |
| last_interaction      | datetime         | YES  |     | NULL                |                |
| last_interaction_user | varchar(255)     | YES  |     | NULL                |                |
| location_id           | int(11)          | YES  |     | NULL                |                |
+-----------------------+------------------+------+-----+---------------------+----------------+

1 个答案:

答案 0 :(得分:0)

作为临时解决方案,试试这个:

$recipientObject = new Recipient(
    [
        'firstname'             => $recipientArray['firstname'],
        'lastname'              => $recipientArray['lastname'],
        'dob'                   => $recipientArray['dob'],
        'last_sent'             => null,
        'last_visit'            => null,
        'source'                => $recipientArray['source'],
        'last_attendance'       => null,
        'last_attendance_class' => '',
        'last_interaction'      => null,
        'last_interaction_user' => '',
    ]
);
$recipientObject->location_id > $locationObject->id;
recipientObject->save();

这应该可行,但它不能解决您遇到的问题。如果您希望我进一步调查,请粘贴整个收件人模型