Hello Christian,
The beauty of the accessor is that you don't need to call the function but the attribute you want to have. In your view you can use:
<p> {{ $thread->getFirstPost()->user->name }} {{ $thread->created_at }} </p>
What happen with your code is that getCreatedAtAttribute is called twice, first with the real attribute value and then with the result from the first call.
You get the error because Carbon can't parse the result from the diffForHumans to use it again.
See the docs for more information: https://laravel.com/docs/8.x/eloquent-mutators#defining-an-accessor
xdesertx liked this reply
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community