如何解决拉威尔中非客体属性的获取问题? [英] How to Solve "Trying to get property of non-object" in Laravel?
本文介绍了如何解决拉威尔中非客体属性的获取问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我的代码返回空,则会生成此错误。如果代码返回一些数据,则运行正常。
控制器
$profile_data= DB::table('partner_prefence')
->select('*')
->where('profile_id',$profile_id)
->first();
return view('partner_prefence',['profile_data' => $profile_data]);
查看/刀片
@php($rel_status = explode(',', $profile_data->p_marital_status))
如果$profile->p_marital_status
有一个值,则没有问题。仅当其值为空时才会出现错误。
推荐答案
使用optional()
https://laravel.com/docs/5.8/helpers#method-optional
如果给定对象为Null,则属性和方法将返回Null而不是导致错误。
{!! old('name', optional($user)->name) !!}
这篇关于如何解决拉威尔中非客体属性的获取问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文