流明禁用碳日期
Lumen disable carbon date
我需要记录日期而不是碳实例。
$soldier = Soldier::find($id);
dd($soldier->soldier_data->pluck('created_at'));
运行 此代码将输出:
object(Illuminate\Support\Collection)#71 (1) { ["items":protected]=> array(4) { [0]=> object(Carbon\Carbon)#66 (3) { ["date"]=> string(26) "2017-08-03 13:27:47.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } [1]=> object(Carbon\Carbon)#65 (3) { ["date"]=> string(26) "2017-08-03 13:28:13.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } [2]=> object(Carbon\Carbon)#77 (3) { ["date"]=> string(26) "2017-08-03 13:28:15.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } [3]=> object(Carbon\Carbon)#63 (3) { ["date"]=> string(26) "2017-08-03 13:28:15.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } } }
this return created_at
作为 carbon 实例。我也将 dates
数组留空。但没有机会。
class SoldierData extends Model {
protected $fillable = [];
protected $dates = [];
protected $table = 'soldier_data';
修复
可以直接在SoldierData
class.
中施放
protected $casts = [
'created_at' => 'string',
];
原因
dates
属性不影响施法的原因如下。 Eloquent class HasAttribute
包含默认值 dates
:protected $dates = [];
。和 getDates
方法
public function getDates()
{
$defaults = [static::CREATED_AT, static::UPDATED_AT];
return $this->usesTimestamps() ? array_merge($this->dates, $defaults) : $this->dates;
}
所以这两个属性 created_at
和 updated_at
默认情况下被转换为没有任何定义的日期。然后看一下attributesToArray
:前两个date被强制转换为date,然后就可以覆盖了。
public function attributesToArray()
{
// If an attribute is a date, we will cast it to a string after convert$
// to a DateTime / Carbon instance. This is so we will get some consist$
// formatting while accessing attributes vs. arraying / JSONing a model.
$attributes = $this->addDateAttributesToArray(
$attributes = $this->getArrayableAttributes()
);
$attributes = $this->addMutatedAttributesToArray(
$attributes, $mutatedAttributes = $this->getMutatedAttributes()
);
// Next we will handle any casts that have been setup for this model an$
// the values to their appropriate type. If the attribute has a mutator$
// will not perform the cast on those attributes to avoid any confusion.
$attributes = $this->addCastAttributesToArray(
$attributes, $mutatedAttributes
);
此方法 attributesToArray
是从 Eloquent\Model::toArray
方法调用的。那就是打字的内厨。
我需要记录日期而不是碳实例。
$soldier = Soldier::find($id);
dd($soldier->soldier_data->pluck('created_at'));
运行 此代码将输出:
object(Illuminate\Support\Collection)#71 (1) { ["items":protected]=> array(4) { [0]=> object(Carbon\Carbon)#66 (3) { ["date"]=> string(26) "2017-08-03 13:27:47.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } [1]=> object(Carbon\Carbon)#65 (3) { ["date"]=> string(26) "2017-08-03 13:28:13.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } [2]=> object(Carbon\Carbon)#77 (3) { ["date"]=> string(26) "2017-08-03 13:28:15.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } [3]=> object(Carbon\Carbon)#63 (3) { ["date"]=> string(26) "2017-08-03 13:28:15.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } } }
this return created_at
作为 carbon 实例。我也将 dates
数组留空。但没有机会。
class SoldierData extends Model {
protected $fillable = [];
protected $dates = [];
protected $table = 'soldier_data';
修复
可以直接在SoldierData
class.
protected $casts = [
'created_at' => 'string',
];
原因
dates
属性不影响施法的原因如下。 Eloquent class HasAttribute
包含默认值 dates
:protected $dates = [];
。和 getDates
方法
public function getDates()
{
$defaults = [static::CREATED_AT, static::UPDATED_AT];
return $this->usesTimestamps() ? array_merge($this->dates, $defaults) : $this->dates;
}
所以这两个属性 created_at
和 updated_at
默认情况下被转换为没有任何定义的日期。然后看一下attributesToArray
:前两个date被强制转换为date,然后就可以覆盖了。
public function attributesToArray()
{
// If an attribute is a date, we will cast it to a string after convert$
// to a DateTime / Carbon instance. This is so we will get some consist$
// formatting while accessing attributes vs. arraying / JSONing a model.
$attributes = $this->addDateAttributesToArray(
$attributes = $this->getArrayableAttributes()
);
$attributes = $this->addMutatedAttributesToArray(
$attributes, $mutatedAttributes = $this->getMutatedAttributes()
);
// Next we will handle any casts that have been setup for this model an$
// the values to their appropriate type. If the attribute has a mutator$
// will not perform the cast on those attributes to avoid any confusion.
$attributes = $this->addCastAttributesToArray(
$attributes, $mutatedAttributes
);
此方法 attributesToArray
是从 Eloquent\Model::toArray
方法调用的。那就是打字的内厨。