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"
"...
]
...
}
]
}
...
}
How are you displaying the date? Show us the template code.
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')
: ''}}
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') : ''}}
@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.
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.
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
Also, adding the "->" like "start_date->formatLocalized" the date dont appears.
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
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);
With only:
{{ $draftConference->conference->start_date->formatLocalized('%a, %b %d, %Y - %H:%M'): ''}}
shows "ErrorException (E_ERROR)
Trying to get property of non-object".
Humor me and try
@foreach ($draftConferences as $conference)
{{ !empty($conference->start_date) ? $conference->start_date->formatLocalized('%a, %b %d, %Y - %H:%M') : '' }}
@endforeach
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
@rgb -one It's basically what I just posted except using an if lol
Please sign in or create an account to participate in this conversation.