在 Views laravel 上使用 carbon

Use carbon on Views laravel(在 Views laravel 上使用 carbon)
本文介绍了在 Views laravel 上使用 carbon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想在视图上使用 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)}} 转换为人类可读的格式

I just get errors. I want to convert the {{($mytask->logon)}} to human readable format using carbon

推荐答案

EDIT 2021-10-29

看来 Laravel 现在鼓励使用这个问题的日期转换:

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 文档 的内容,供 googlers 添加可以将您的 SQL 日期时间字段转换为 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'];

这个数组上的所有字段都可以在你的视图中使用 Carbon 函数自动访问,例如:

All the fields present on this array will be automatically accessible in your views with Carbon functions like:

{{ $article->mydate->diffForHumans() }}

这篇关于在 Views laravel 上使用 carbon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Warning: mysqli_query() expects at least 2 parameters, 1 given. What?(警告:mysqli_query() 需要至少 2 个参数,1 个给定.什么?)
INSERT query produces quot;Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean givenquot;(INSERT 查询产生“警告:mysqli_num_rows() 期望参数 1 为 mysqli_result,给出布尔值;)
prepared statements - are they necessary(准备好的陈述 - 它们是否必要)
Do I need to escape my variables if I use MySQLi prepared statements?(如果我使用 MySQLi 准备好的语句,是否需要转义我的变量?)
Properly Escaping with MySQLI | query over prepared statements(使用 MySQLI 正确转义 |查询准备好的语句)
Is it possible to use mysqli_fetch_object with a prepared statement(是否可以将 mysqli_fetch_object 与准备好的语句一起使用)