亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 null 上調用成員函數 diffForHumans()(視圖:

在 null 上調用成員函數 diffForHumans()(視圖:

PHP
不負相思意 2023-04-02 14:49:51
我有熱點新聞按鈕可以將帖子設為熱點新聞當我將帖子設為熱點新聞時它工作正常但是當我沒有任何熱點新聞帖子時它會出現此錯誤。<div class="feature_article_inner">    <div class="tag_lg red">        <a href="{{ url('/details') }}/{{ $hot_news['slug'] }}">Hot News</a>    </div>    <!-- feature_article_title -->    <div class="feature_article_title">        <h1><a href="{{ url('/details') }}/{{ $hot_news['slug'] }}" target="_self">{{ $hot_news['title'] }} </a></h1>    </div>    <!-- feature_article_date -->    <div class="feature_article_date">        by: <a href="{{ url('/author') }}/{{ $hot_news['creator']['id'] }}">{{ $hot_news['creator']['name'] }}</a> ,        &nbsp;{{ $hot_news['created_at']->diffForHumans() }} </a></div>    <div style="font-size: 9.5pt; color: black; "> Views: {{ $hot_news['view_count'] }} </div>    <!-- feature_article_content -->    <div class="feature_article_content">        {{ $hot_news['short_description'] }}    </div>    <!-- article_social -->    <div class="article_social">        <span>            <i class="fas fa-comment"></i>            <a href="{{ url('/details') }}/{{ $hot_news['slug'] }}">                {{ $hot_news['comments_count'] }} Comments            </a>        </span>    </div></div>
查看完整描述

2 回答

?
侃侃無極

TA貢獻2051條經驗 獲得超10個贊

查詢構建器將日期作為字符串返回,您需要在使用它們之前進行解析,否則您將收到類似 PHP 錯誤的錯誤。

解決方案 :

因為您的 created_at 是一個字符串,所以您必須解析它以使 Carbon 工作并修復它調用成員函數

{{\Carbon\Carbon::parse($hot_news['created_at'])->diffForHumans() }}


查看完整回答
反對 回復 2023-04-02
?
牧羊人nacy

TA貢獻1862條經驗 獲得超7個贊

盡管 Muhammad 的回答是正確的,但我通常將這種類型的轉換直接添加到我的 Model::class 中。這將確保我的解析日期的代碼永遠不會重復。所以將來如果我想改變日期格式,我只需要在這里改變它就可以了。


use Carbon\Carbon;


public function getCreatedAtAttribute($date)

{

? ? return Carbon::parse($date)->diffForHumans();

}


查看完整回答
反對 回復 2023-04-02
  • 2 回答
  • 0 關注
  • 130 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號