我正在调试laravel项目,并且在很多情况下,开发人员只是在引用对象属性的 Blade 模板中放入了双 mustache ,而没有先检查这些关系是否存在。
例
{{$category->name}}
<small>{{$category->parent->name}}</small>
如果没有设置$ category或未设置$ category-> parent,laravel中是否有内置的方式来使失败默默地失败,而无需遵循这些原则...
@if (isset($category))
{{$category->name}}
@endif
@if (isset($main_category->parent))
@if($main_category->parent != null)
<small>{{$main_category->parent->name}}</small>
@endif
@endif
最佳答案
有一个更好的办法。
{{ $category->name ?? '' }}
// or
{{ $category->name or '' }} // for older version of laravel
关于php - 在 Blade 模板中捕获 “Trying to get property of non-object”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56079224/