Home > Blockchain >  Diff for humans not working in laravel blade
Diff for humans not working in laravel blade

Time:05-03

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

CodePudding user response:

$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

CodePudding user response:

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

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

  • Related