Use carbon on Views laravel
Asked Answered
T

6

19

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

Tswana answered 28/11, 2014 at 2:13 Comment(3)
What's ->logon ? Is it mutated to carbon in your Eloquent model using getDates() or accessor?Sargassum
What's the solution?Dickinson
Can't you pass in a carbon object variable via the controller? It would save you a whole mess.Croon
I
37

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() }}
Inky answered 24/10, 2015 at 12:3 Comment(4)
Heads up for the hint !Mowbray
Sweet. This is better than handling in the controllerNonagenarian
Very much preferred as simple and cleaner.Rhythmist
this will not work for me because I am getting data from an sql server stored procedureKarilla
H
18

You need not add a use statement for carbon in the view. Just make sure that $mytask->logon is indeed a carbon object and use the format() method to turn it into a string

{{ $mytask->logon->format('Y/m/d') }}

Edit:

If $mytask->logon is a carbon object use:

 {{ $mytask->logon->diffForHumans() }}

If it's still a string use:

{{  \Carbon\Carbon::createFromTimeStamp(strtotime($mytask->logon))->diffForHumans()‌​ }}

I would advise to do that in the controller though or a view composer to keep your view neat.

Hindenburg answered 28/11, 2014 at 2:27 Comment(3)
Yes sir the $mytask->logon is in timestamp format, but what i want to do is to use the second,minutes or hours ago. For example 5mins ago, an hour ago. Just like that sirTswana
If $mytask->logon is a carbon object use: $mytask->logon->diffForHumans() If it's still a string use: \Carbon\Carbon::createFromTimeStamp(strtotime($mytask->logon))->diffForHumans();. I would advise to do that in the controller though or a view composer to keep your view neat.Hindenburg
Just keeps getting eroor, when I use any of the said answers in the views. :(Tswana
B
13

Blade Use:

{{ \Carbon\Carbon::parse($mytask->logon)->diffForHumans() }}

Output: For a task that is one day ago from now

1 day ago

More for Human readable time by Carbon you can read - Carbon Difference For Humans

Borroff answered 5/11, 2017 at 18:8 Comment(0)
C
3

Just Copy Paste it into your Model

public function getCreatedAtAttribute($value)
{
    $date = new Carbon($value);
    return $date->toDayDateTimeString();
}

And called ->created_at like you normally called in your view.

Don't forget to use the Carbon Class Model

Condor answered 31/10, 2018 at 21:37 Comment(0)
B
1

For laravel 5 Note if you need to do some custom mutations, chuck this in your model.

 /**
 * The string attribute that should be cast to custom carbon date.
 *
 * @var array
 */

public function getTimeAttribute()
{
     return Carbon::createFromTimestampUTC($this->attributes['time']/1000);
}

Dont worry you can still access the original attribute.

    New = {{ $event->time }} Original = {{ $event->getOriginal('time')}}

Hope this helps someone who cannot use the standard way mentioned.

Beaton answered 13/1, 2016 at 1:28 Comment(1)
This was very helpfullMowbray
K
1

{{\Carbon\Carbon::parse($mytask->logon)->format('Y-m-d')}}

Killam answered 15/2, 2022 at 16:7 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Nonfeasance

© 2022 - 2024 — McMap. All rights reserved.