比laravel函数optional更简洁的写法
optional
函数接受任何参数,并允许你访问该对象上的属性或调用其方法。如果给定对象为 null
,属性或方法将返回 null
而不是引发错误:Attempt to read property "street" on null
return optional($user->address)->street;
而更简洁的写法:
return $user->address?->street;
推荐文章: