简体   繁体   English

Laravel 5:jenssegers / laravel-date没有显示正确的翻译。 如何正确使用Laravel Date?

[英]Laravel 5: jenssegers/laravel-date doesn't show correct translation. How do I use Laravel Date correctly?

I have a simple L5 application, where I have to output some dates in a different language and therefore I am using jenssegers/laravel-date package 我有一个简单的L5应用程序,我必须用其他语言输出一些日期,因此我正在使用jenssegers / laravel-date包

Yet, if I try to use jenssegers new package on my data stored in my database, I don't get the translation, see here: 但是,如果我尝试对存储在数据库中的数据使用jenssegers新软件包,则无法获得翻译结果,请参见此处:

index.blade.php index.blade.php

@extends('app')

@section('content')

use Jenssegers\Date\Date;
// This returns 'vor 0 Sekunden' in German, which I do want.
echo Date::now()->diffForHumans();

echo '<br/>';

// This returns '1 day ago'. It is in English, but I need it in German.
echo $orders->find(1)->created_at->diffForHumans();

@endsection

Would appreciate any help. 将不胜感激。

The reason for this to happen is that $orders->find(1)->created_at returns a Carbon instance, not a Date one. 发生这种情况的原因是$orders->find(1)->created_at返回一个Carbon实例,而不是Date实例。

One approach to leverage Date is by doing it: 利用Date一种方法是:

echo Date::instance($orders->find(1)->created_at)->diffForHumans();

You can use an acessor to always return a Date object, like so: 您可以使用acessor始终返回Date对象,如下所示:

at your Order model: 在您的订单模型上:

public function getCreatedAtAttribute($value)
{
    return Date::instance($value);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM