我在mysql数据库中有一个timestamp变量列.试图将碳时间戳转换为我可以在那里输入的内容,但Carbon :: Now()只返回一个Carbon对象,当我尝试使用Carbon对象的时间戳字符串时,它不会在MysqL中注册.
public function store(CreateArticleRequest $request){
$input = $request->all();
var_dump($input); // JUST SO YOU CAN SEE
$input['published_at'] = Carbon::Now();
var_dump($input); // JUST SO YOU CAN SEE
Article::create($input);
}
我的第一个var转储是这样的:
array (size=4)
'_token' => string 'Wy67a4hWxrnfigz61wmXfYCSjAdldv26wOJiLWNc' (length=40)
'title' => string 'ASDFasdf' (length=8)
'body' => string 'asdfasdf' (length=8)
'published_at' => string '2015-08-26' (length=10)
我的第二个var转储是这样的.
与“published_at”相关的MysqL列是时间戳变量.我想如何从碳对象转换它?
提前致谢.
解决方法:
您还可以在模型上设置Mutator.
public function setPublishedAt($value)
{
$this->attributes['published_at'] = strtotime($value);
}
转换为时间戳
$model -> setPublishedAt('2015-08-26'); // 1440572400
或者您可以使用strtotime将日期转换为时间戳
strtotime — Parse about any English textual datetime description into a Unix timestamp
希望这有帮助.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。