Php 测试使用默认值为NOTNULL字段返回null

Php 测试使用默认值为NOTNULL字段返回null,php,laravel,laravel-5,Php,Laravel,Laravel 5,我有下面的代码:我用它一个简单的方法来驱动功能集表的列,所以我想空调,游泳池。。使用默认为0的布尔值进行etc 但是,当我使用工厂或直接实例化它时,如下面所示->空调始终为空,我不知道为什么。我使用mysql数据库进行了测试,当我添加新行时,它默认为正确的0。如果我在这里遗漏了什么,为什么不在模型实例上设置0的默认值呢 class FeatureSetTest extends TestCase { use RefreshDatabase; public function testCa

我有下面的代码:我用它一个简单的方法来驱动功能集表的列,所以我想空调,游泳池。。使用默认为0的布尔值进行etc

但是,当我使用工厂或直接实例化它时,如下面所示->空调始终为空,我不知道为什么。我使用mysql数据库进行了测试,当我添加新行时,它默认为正确的0。如果我在这里遗漏了什么,为什么不在模型实例上设置0的默认值呢

class FeatureSetTest extends TestCase
{
    use RefreshDatabase;
  public function testCanCreateFeatureSetAndValuesDefaultToFalse()
   {
       $featureSet = new FeatureSet(['property_id' => '1']);
       $featureSet->save();

       $this->assertFalse($featureSet->air_conditioning);
   }
}

如果在测试中保存$featureSet后添加$featureSet,我会得到:

 #attributes: array:4 [
   "property_id" => "1"
   "updated_at" => "2018-05-12 16:15:19"
   "created_at" => "2018-05-12 16:15:19"
   "id" => 1
 ]
如果我执行dd(DB::select(DB::raw('SHOW CREATE TABLE feature_set'));它输出以下值,因此将0设置为默认值,并且:

 CREATE TABLE `feature_sets` (\n
           `id` int(10) unsigned NOT NULL AUTO_INCREMENT,\n
           `property_id` int(10) unsigned NOT NULL,\n
           `air_conditioning` tinyint(1) NOT NULL DEFAULT '0',\n
           `created_at` timestamp NULL DEFAULT NULL,\n
           `updated_at` timestamp NULL DEFAULT NULL,\n
           PRIMARY KEY (`id`)\n
         ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_c
最后,如果我放入dd(DB::select(DB::raw('select*FROM feature_set'));在我的测试中,我可以看到默认值设置正确:

array:1 [
  0 => {#598
    +"id": 1
    +"property_id": 1
    +"air_conditioning": 0
    +"created_at": "2018-05-12 15:44:31"
    +"updated_at": "2018-05-12 15:44:31"
  }
]
//特征集模型

<?php

namespace App\Models\Property;

use Illuminate\Database\Eloquent\Model;

class FeatureSet extends Model
{
    protected $guarded = [];

    public function asFeaturesArray()
    {

        return [
            'air_conditioning' => [
                'title' => __('Air Conditioning'),
                'value' => $this->air_conditioning
            ]
        ];
    }
}

结果是,
$featureSet->refresh()
就是这个问题的答案,
->fresh()
只需获取模型中现有属性的新值,其中作为refresh获取所有属性的新版本,其中包括默认为0的属性

fresh=从数据库重新加载一个新的模型实例。 refresh=从数据库中重新加载具有新属性的当前模型实例

在这里找到了解决方案-

<?php

namespace App\Models\Property;

use Illuminate\Database\Eloquent\Model;

class FeatureSet extends Model
{
    protected $guarded = [];

    public function asFeaturesArray()
    {

        return [
            'air_conditioning' => [
                'title' => __('Air Conditioning'),
                'value' => $this->air_conditioning
            ]
        ];
    }
}