laravel - 你知道为什么会出现这个错误吗?(在 null 上调用成员函数 formatLocalized())
问题描述
我有一个具有 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>
解决方案
推荐阅读
- google-app-engine - NoClassDefFoundError:com/google/appengine/api/search/IndexSpec
- python-3.x - 从 chrome 浏览器本地存储中提取并保存 whatsapp 会话,然后在另一台设备上使用它
- python-3.x - python3 - 当父类在单独的文件中时,子类不继承父方法
- batch-file - 在批处理脚本中编辑 ini 文件中的特定键值对
- spring - Spring WebSocket:如何与在房间聊天的用户制作一个简单的应用程序?
- c# - 使 UI 元素跟随相机
- java - 如何将哈希密码存储在数据库中?
- android - Rxjava 分页递归 - RxJava
- android - 找不到满足版本约束的“com.android.support:support-compat”版本
- wordpress - admin_enqueue_scripts 未登录时不加载任何文件