![](/img/trans.png)
[英]Laravel - How to call model to class Helper then display the result in view
[英]How can I call a function of a helper class in a view - Laravel 5.8?
我有一個幫助類DateHelper
我在那里有一個fn
public static function getAgo($date) {
if ($date) {
$ts = time() - strtotime(str_replace("-","/",$date));
if($ts>31536000) $val = round($ts/31536000,0).' year';
else if($ts>2419200) $val = round($ts/2419200,0).' month';
else if($ts>604800) $val = round($ts/604800,0).' week';
else if($ts>86400) $val = round($ts/86400,0).' day';
else if($ts>3600) $val = round($ts/3600,0).' hour';
else if($ts>60) $val = round($ts/60,0).' minute';
else $val = $ts.' second';
if($val>1) $val .= 's';
return $val;
}
}
我希望在我看來像這樣使用它
{{ DateHelper::getAgo($log->createdAt) }}
我不斷得到
找不到“DateHelper”類(查看:...
我試着將它包含在我的index.blade.php
<?php use App\DateHelper; ?>
有人可以幫幫我嗎 ?
您可以在config/app.php
添加別名:
'aliases' => [
// ...
'DateHelper' => App\DateHelper::class,
],
然后在您的視圖中,您可以簡單地執行:
{{ DateHelper::getAgo($log->created_at); }}
如果use
語句在視圖中不起作用,則始終可以使用內聯命名空間:
{{ \App\DateHelper::getAgo($log->created_at) }}
確保該類位於正確的命名空間中並具有正確的類/文件名,您應該很高興。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.