Whitelionx's avatar

Error Call to a member function diffForHumans() on null

I am extremely new to laravel and I'm having an issue "Error Call to a member function diffForHumans() on null (View: D:\www\site\local\resources\views\welcome.blade.php)"

[code]@extends('layouts.app') @section('content') Join Now! Best server, awesome battles with your friends! Join Now! Best server, awesome battles with your friends! Join Now! Best server, awesome battles with your friends! Join Now! Best server, awesome battles with your friends! Last News All news @foreach($news->take(3) as $new) {{$new->title}} {{$new->created_at->diffForHumans()}} Read more @endforeach @include('includes.aside')

</div><!--mainHomeBlock-->

<span class="line"></span>
<div class="sliderGallery">
    <div class="slider-gallery">
        <div class="sliderImg"><a href=""><img src="{{asset('images/img_1.jpg')}}" alt="img_1"></a></div>
        <div class="sliderImg"><a href=""><img src="{{asset('images/img_2.jpg')}}" alt="img_1"></a></div>
        <div class="sliderImg"><a href=""><img src="{{asset('images/img_3.jpg')}}" alt="img_1"></a></div>
        <div class="sliderImg"><a href=""><img src="{{asset('images/img_4.jpg')}}" alt="img_1"></a></div>
        <div class="sliderImg"><a href=""><img src="{{asset('images/img_1.jpg')}}" alt="img_1"></a></div>
        <div class="sliderImg"><a href=""><img src="{{asset('images/img_2.jpg')}}" alt="img_1"></a></div>
        <div class="sliderImg"><a href=""><img src="{{asset('images/img_3.jpg')}}" alt="img_1"></a></div>
        <div class="sliderImg"><a href=""><img src="{{asset('images/img_4.jpg')}}" alt="img_1"></a></div>
    </div>
</div>

@endsection[/code]

D:\www\site\local\resources\views/welcome.blade.php:59

0 likes
6 replies
Sergiu17's avatar

$new->created_at is null, and you are trying to call a method on null which results in an error. Check why created_at is null in the first place, as a solution use nullsafe opeartor

{{ $new->created_at?->diffForHumans() ?? 'Unknown date' }}
1 like
tykus's avatar

Rather than mitigating like this, the first question I would be asking myself is why the model(s) created_at attribute is null? The timestamps attributes should have been populated automatically. Fix the underlying issue, not the symptom.

Whitelionx's avatar

It's a public source that I'm using I'm not that greatly versed in PHP let alone laravel :( It now works the website now shows not that I will ever use the news system though

Whitelionx's avatar

I obviously would fix the issue if I knew what the problem was or where to even look. All it has to do is read the MySQL to get the data, which it actually does apart from showing the news content itself.

Please or to participate in this conversation.