問題描述
我想在視圖上使用 Carbon,我將它包含在視圖文件的頂部,但它不起作用,我就是這樣做的.
I want to use the Carbon on Views I'm including it on the top of the views file but it doesnt work, I'm doing it like this.
<?php use carbon/carbon;?>
@extends('main_layout')
@foreach ($myquery as $mytask)
<tr>
<td >
{{($mytask->firstname)}}
</td>
<td >
{{($mytask->lastname)}}
</td>
<td>
{{($mytask->logon)}}
</td>
@section('content')
@stop
我只是遇到錯誤.我想使用 carbon 將 {{($mytask->logon)}} 轉(zhuǎn)換為人類可讀的格式
I just get errors. I want to convert the {{($mytask->logon)}} to human readable format using carbon
推薦答案
EDIT 2021-10-29
看來 Laravel 現(xiàn)在鼓勵使用這個問題的日期轉(zhuǎn)換:
It appears that Laravel now encourages the use of date casts for this question:
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'created_at' => 'datetime:Y-m-d',
];
原答案
我會添加一些引用 Laravel 文檔 的內(nèi)容,供 googlers 添加可以將您的 SQL 日期時間字段轉(zhuǎn)換為 Carbon 對象:
I would add sommething quoting Laravel Documentation for googlers to add how you can transform your SQL datetime fields into Carbon objects:
在您的模型中:
protected $dates = ['created_at', 'updated_at', 'disabled_at','mydate'];
這個數(shù)組上的所有字段都可以在你的視圖中使用 Carbon 函數(shù)自動訪問,例如:
All the fields present on this array will be automatically accessible in your views with Carbon functions like:
{{ $article->mydate->diffForHumans() }}
這篇關(guān)于在 Views laravel 上使用 carbon的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!