php Laravel 错误:在字符串上调用成员函数 format()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40533377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel error: Call to a member function format() on string
提问by zwl1619
I am using Laravel 5.3
.
我正在使用 Laravel 5.3
。
There is a field expired_at
in table articles
:
expired_at
表中有一个字段articles
:
public function store(Request $request)
{
$data=[
'expired_at'=>Carbon::now()->addDays(30)->endOfDay()
];
$article=Article::create(array_merge($request->all(),$data));
return redirect('/artilces');
}
view:
看法:
{{$article->expired_at->format('Y-m-d')}}
error:
错误:
Call to a member function format() on string (View: D:\wnmp\www\laravel-5-3-dev\resources\views\artiles\index.blade.php)
在字符串上调用成员函数 format()(视图:D:\wnmp\www\laravel-5-3-dev\resources\views\artiles\index.blade.php)
Why is it?
为什么?
回答by Amit Gupta
回答by Nazmul Hasan
I think this is the way. It will not throw error
我认为这就是方法。它不会抛出错误
{{ Carbon\Carbon::parse($article->expired_at)->format('Y-m-d') }}
回答by Javier dc
If you use DB:: to retrieve complex data, you can′t use mutators. In this situation I use this:
如果你使用 DB:: 来检索复杂的数据,你就不能使用 mutator。在这种情况下,我使用这个:
{{ date('m-Y', strtotime($hora->Fecha)) }}
Or
或者
{{ date('m-Y', strtotime($hora ?? ''->Fecha)) }}
Where "Fecha" is a datetime value.
其中“Fecha”是日期时间值。