【发布时间】:2018-05-09 20:05:22
【问题描述】:
我有一个具有 index() 方法的 UserController,该方法应该获取用户过去在会议中的所有注册,并为每次注册获取会议详细信息:
我有以下代码来获取用户在会议中的过去和下一次注册:
public function index(Request $request){
$user = $request->user();
$pastRegistrations = $user->registrations()->with(['conference' => function ($query) {
$query->where('conf_end_date', '<', now());
}])->get();
$nextRegistrations = $user->registrations()->with(['conference' => function ($query) {
$query->where('conf_end_date', '>', now());
}])->get();
return view('users.index',
compact('user', 'pastRegistrations','nextRegistrations'));
}
要在 index.blade.php 中显示,有这个 foreach:
@foreach($nextRegistrations as $nextRegistration)
<li class="list-group-item">
<p {{optional($nextRegistration->conf)->conf_start_date->formatLocalized('%a, %b %d, %Y - %H:%M')}}</p>
<h5>{{optional($nextRegistration->conf)->title}}</h5>
<p Registration in {{$nextRegistration->conf->created_at }}</p>
</li>
@endforeach
但是显示结果时 $nextRegistrations 出现错误:
Call to a member function formatLocalized() on null
在
<?php echo e(optional($nextRegistration->conf)->conf_start_date->formatLocalized('%a, %b %d, %Y - %H:%M')); ?></p>
问题只是:
<p {{optional($nextRegistration->conf)->conf_start_date->formatLocalized('%a, %b %d, %Y - %H:%M')}}</p>
只有这个有效:
<h5>{{optional($nextRegistration->conf)->title}}</h5>
<p Registration in {{$nextRegistration->conf->created_at }}</p>
【问题讨论】:
标签: laravel