Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use carbon on Views laravel

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

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

like image 336
jake balba Avatar asked Nov 28 '14 02:11

jake balba


People also ask

How do you use Carbon function in Laravel?

The simplest use of the Carbon class is to read the current date and time. Open the datetimeController. php file and replace the content with the following script. The now() function of the Carbon class has been used in the script to read the current date and time.

How do you use the Carbon class in Laravel blade?

If you want to use the namespaced class, you don't need the first slash: $create_at_difference=Carbon\Carbon::createFromTimestamp(strtotime($create))->diff(\Carbon\Carbon::now())->days; You should just write Carbon\Carbon instead of \Carbon\Carbon. That is a quick solution.


1 Answers

EDIT 2021-10-29

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',
];

Original Answer

I would add sommething quoting Laravel Documentation for googlers to add how you can transform your SQL datetime fields into Carbon objects:

In your Model:

protected $dates = ['created_at', 'updated_at', 'disabled_at','mydate'];

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

{{ $article->mydate->diffForHumans() }}
like image 64
Hammerbot Avatar answered Oct 13 '22 02:10

Hammerbot