'Diff for humans not working in laravel blade

Hi am trying to use diffForHumans in laravel blade but its not working.please help`enter code here Heres my code

@foreach($students as @student)
   $time=$student->created_at->diffForHumans()
@php echo $time;@endphp @endforeach


Solution 1:[1]

$student->created_at is a string, hence you can't call diffForHumans() function on it. Instead, you should parse it via Carbon instead. Here's how to do it

@foreach($students as @student)
    @php
        $time= \Carbon::parse($student->created_at)->diffForHumans()
    @endphp
    //Now do what you need to do
@endforeach

Also, you can't just write php code inside blade. If you do that, you need to contain the code inside @php tags as shown above. Although, it's not really clean to write php code in blade. With that being said, if you just want to display the time, here's how I would do it

@foreach($students as @student)
    <p>Created At : {{ \Carbon::parse($student->created_at)->diffForHumans() }}</p>
@endforeach

Solution 2:[2]

All You need is to parse the date using Carbon like this

{{ Carbon\Carbon::parse($student->created_at)->diffForHumans() }}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ronodip Basak
Solution 2 Joseph