Laravel 避免 Trying to get propert
Laravel 避免 Trying to get property of non-object 的处理办法
1. 使用isset判断
$email = isset($blog->user->email) ? $blog->user->email : 'no email';
// blog模板中
{{ $blog->user->email or 'no email' }}
2. 如果php7版本 可以使用??
$email = $blog->user->email ?? 'no email';
3. 使用object_get函数
$email = object_get($blog->user, 'email', 'no email');
4. 使用data_get函数
$email = data_get($blog->user, 'email', 'no email');
本文标题:Laravel 避免 Trying to get propert
本文链接:https://www.haomeiwen.com/subject/rwrzmhtx.html
网友评论