2016-11-10 53 views
10

我正在使用Laravel 5.3Laravel錯誤:調用字符串上的成員函數格式()

有表articlesexpired_at

public function store(Request $request) 
{ 
    $data=[ 
     'expired_at'=>Carbon::now()->addDays(30)->endOfDay() 
    ]; 
    $article=Article::create(array_merge($request->all(),$data)); 

    return redirect('/artilces'); 
} 

觀點:

{{$article->expired_at->format('Y-m-d')}} 

錯誤:

Call to a member function format() on string (View: D:\wnmp\www\laravel-5-3-dev\resources\views\artiles\index.blade.php) 

爲什麼

回答

12

在你Article類添加以下屬性:

/** 
* The attributes that should be mutated to dates. 
* 
* @var array 
*/ 
protected $dates = ['expired_at']; 

Docs

+1

這是正確答案 –

9

我認爲這是這樣的。它不會拋出錯誤

{{ Carbon\Carbon::parse($article->expired_at)->format('Y-m-d') }} 
3

如果你使用DB ::來檢索複雜的數據,你不能使用增變器。 在這種情況下,我用這個:

{{ date('m-Y', strtotime($hora->Fecha)) }} 

其中,「出生日期」是一個datetime值。

相關問題