laravel Blade 是他们的一个巧妙解决方案:试图获得 属性 的非对象

laravel Blade is their a neat solution to : Trying to get property of non-object

我只有一行代码

{{($user->company_privileges->level < 3 ? '' : ' disabled') }}

在我的菜单文件中

但是当用户没有公司时我收到错误

Trying to get property of non-object

他们是在一行中处理这个问题的巧妙方法吗?

想到了这样的事情

{{ ($user->company_privileges === null) : '' ? ($user->company_privileges->level < 3 ? '' : ' disabled') ) }}

因为第二个 if 语句只有 运行 如果用户有公司

我在标签中也有声明

<a class="{{($user->company_privileges->level < 3 ? '' : ' disabled') }}">

为什么不像

<a class="{{($user->company_privileges && $user->company_privileges->level < 3 ? '' : ' disabled') }}">

本质上,您在这里需要的是知道 null == false 用于 bool 比较。查看 PHP type comparisons table