Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

adamjhn's avatar

Date doesn't appear in view

I have this code to show the conference start date if is not null, if is null show ''. The issue is that in the database the start_date field is not null bot on the page the start date dont appears. Do you know what can be the issue?

     {{!empty($draftConference->conference->
       start_date) ? 
       $draftConference->conference->
       start_dateformatLocalized('%a, %b %d, %Y - %H:%M')
                : ''}}


$draftConferences shows like:

    LengthAwarePaginator {#341 ▼
      #total: 1
      #lastPage: 1
      #items: Collection {#340 ▼
        #items: array:1 [▼
          0 => Conference {#329 ▼
            ...
            #attributes: array:22 [▼
              "id" => 2
              "name" => "test"
              "description" => ""
              "start_date" => "2018-10-30 08:30:00"
              "...
            ]
          ...
          }
        ]
      }
     ...
    }

0 likes
14 replies
ejdelmonico's avatar

How are you displaying the date? Show us the template code.

adamjhn's avatar

The date is displayed like that, it shows nothing if is null or the date if is not null:

     {{!empty($draftConference->conference->
       start_date) ? 
       $draftConference->conference->
       start_dateformatLocalized('%a, %b %d, %Y - %H:%M')
                : ''}}
Cronix's avatar

You're missing a -> between start_date and formatLocalized.

{{!empty($draftConference->conference->start_date) ? 
       $draftConference->conference->start_date->formatLocalized('%a, %b %d, %Y - %H:%M') : ''}}
2 likes
jlrdw's avatar

@adamjhn once you get this application completely finished it would be so nice of you to put a skeleton of it in GitHub, seriously it would make an excellent tutorial, it seems like a pretty large application.

richbowen's avatar

If it's an array that can potentially have multiple items then step into it first using a foreach then reference each element:

@foreach ($draftConferences as $converence)
    {{ !empty($conference->start_date) ? $conference->start_date->formatLocalized('%a, %b %d, %Y - %H:%M') : '' }}
@endforeach

Updated with some corrections.

adamjhn's avatar

Thanks, but I had a foreach like:


@foreach($draftConferences as $draftConference)
    @if(!empty($draftConference))
    <li class="list-group-item">
        <p> {{!empty($draftConference->conference->start_date) ? $draftConference->conference->start_dateformatLocalized('%a, %b %d, %Y - %H:%M'): ''}}</p>
    @endif      
@enforeach
adamjhn's avatar

Also, adding the "->" like "start_date->formatLocalized" the date dont appears.

Cronix's avatar

From your code I helped with yesterday, you allow nulls in start_date and end_date, so maybe they don't all have one to display in the loop.

do a dd($draftConferences); and check to see if they all have a start_date. It would actually be good to see that dd output, as your loop isn't making sense. I'm not sure you're targeting the right data. Maybe you're just not naming your variables very well, but to me $draftConference would be the conference, so I'm not clear on why you're accessing $draftConference->conference.

Also clear your view cache php artisan view:clear

1 like
adamjhn's avatar

The {{dd($draftConferences)}} before the foreach "@foreach($draftConferences as $draftConference)" shows like:

LengthAwarePaginator {#343 ▼
  #total: 1
  #lastPage: 1
  #items: Collection {#342 ▼
    #items: array:1 [▼
      0 => Conference {#331 ▼
       ...
        #attributes: array:22 [▼
          "id" => 2
          "name" => "test"
          "description" => ""
          "start_date" => "2018-10-30 06:30:00"
          ....
        ]
        ...
      }
    ]
  }
...
}

The query to get the draftConferences:

        $draftConferences = $user->conferences()->where('status','D')->paginate($pageLimit);

adamjhn's avatar

With only:

{{ $draftConference->conference->start_date->formatLocalized('%a, %b %d, %Y - %H:%M'): ''}}

shows "ErrorException (E_ERROR) Trying to get property of non-object".

Cronix's avatar
Cronix
Best Answer
Level 67

Humor me and try

@foreach ($draftConferences as $conference)
    {{ !empty($conference->start_date) ? $conference->start_date->formatLocalized('%a, %b %d, %Y - %H:%M') : '' }}
@endforeach 
2 likes
richbowen's avatar

My bad. My suggestion was incorrect. Here's the correction:

@foreach($draftConferences as $draftConference)
    @if($draftConference->start_date)
    <li class="list-group-item">
        <p>{{ $draftConference->start_date->formatLocalized('%a, %b %d, %Y - %H:%M') }}</p>
    </li>
    @endif      
@enforeach
1 like
Cronix's avatar

@rgb-one It's basically what I just posted except using an if lol

Please or to participate in this conversation.