php - ErrorException (E_ERROR) 试图获取非对象的属性“标题”
问题描述
ErrorException (E_ERROR) 试图获取非对象的属性“标题”(查看:D:\xampp\htdocs\xampp\practice\freecode\resources\views\profiles\index.blade.php)
以前的例外
试图获取非对象的属性“标题”(0)
<div class="d-flex">
<div ><strong>{{ $user->posts->count() }}</strong> posts</div>
<div class="pl-5"><strong>23k</strong> followers</div>
<div class="pl-5"><strong>435</strong> following</div>
</div>
<div class="pt-4 font-weight-bold" ><strong>{{ $user->profile->title }}</strong></div>
<div>{{ $user->profile->description }}</div>
<div><a href="#">{{ $user->profile->url ??'N/A' }}</a></div>
</div>
</div>
<div class="row pt-5">
@foreach($user->posts as $post)
<div class="col-4" >
<img src="/storage/{{ $post->image }}" class="w-100">
</div>
@endforeach
发布后显示此错误。
解决方案
如果您清楚地阅读了错误消息,则表明您正在尝试访问空对象上的标题。这意味着以下关系为空。
$user->profile // returns null
处理此错误的一种快速方法是使用空合并运算符。这将检查任何时候的第一条语句是否返回 null,如果不是,则返回第一条语句。否则它会退回到第二个语句。
{{ $user->profile->title ?? 'user not found'; }}
推荐阅读
- django - AbstractUser 的 UserAdmin 中的额外字段
- go - 为什么从代码中调用 Setenv 不起作用?
- unit-testing - 什么是开发人员的 API 级别测试?
- java - 如何将托管在 ASP.net MVC 网站上的 SignalR 服务器连接到 JAVA Swings 软件客户端
- python - IntEnum 在 _missing_ 中返回缺失的整数会引发 TypeError
- spring - Spring 请求映射正则表达式“不包含”不起作用
- python - 为什么 toTitle 在 Go 中不将小写单词大写?
- c# - “System.ArgumentException:'不存在从对象类型 Newtonsoft.Json.Linq.JValue 到已知托管提供程序本机类型的映射。'
- node.js - Heroku 部署失败?如何正确部署 Angular App + NodeJs?
- laravel - 强制 URL::to('') 在 HTTPS 而不是 HTTP 中打开