Laravel-如何解决:尝试获取属性';id';值为NULL时非对象的

Laravel-如何解决:尝试获取属性';id';值为NULL时非对象的,laravel,Laravel,在我的Laravel-5.8项目中,当我提交以下代码时: public function publish_all_posts(){ $userCompany = Auth::user()->company_id; $userEmployee = Auth::user()->employee_id; $userId = Auth::user()->id; $userEmail = Auth::user()->email; $u

在我的Laravel-5.8项目中,当我提交以下代码时:

public function publish_all_posts(){
    $userCompany = Auth::user()->company_id;
    $userEmployee = Auth::user()->employee_id;    
    $userId = Auth::user()->id;
    $userEmail = Auth::user()->email;
    $userCode = Auth::user()->employee_code;
    $userFirstName = Auth::user()->first_name;
    $userLastName = Auth::user()->last_name;   

    $identities = DB::table('appraisal_identity')->select('id')->where('company_id', $userCompany)->where('is_current', 1)->first();
    $reviewperiods = DB::table('appraisal_identity')->select('appraisal_name')->where('company_id', $userCompany)->where('is_current', 1)->first();
    $reviewperiod = $reviewperiods->appraisal_name;


    $linemanager = DB::table('hr_employees')->where('id', $userEmployee)->first();

    $linemanageruserid = DB::table('hr_employees')->select('line_manager_id')->where('line_manager_id', $linemanager->line_manager_id)->first();
    $linemanageruserids = DB::table('hr_employees')->where('id', $linemanageruserid->line_manager_id)->first();
    $linemanageremails = DB::table('hr_employees')->select('email')->where('id', $linemanageruserids->id)->first();

    $linemanageremail = $linemanageremails->email;

    if ($unapproved_count > 3){
    $unapproved_post = AppraisalGoal::where('employee_id', $userEmployee)->where('appraisal_identity_id', $identities->id)->where('is_published',0)
            ->update([
                'is_published' => 1,
                'is_approved' => 1

                ]);

    $unapproved_post = AppraisalGoal::where('employee_id', $userEmployee)->where('appraisal_identity_id', $identities->id)->where('is_published',1)->first();


        Session::flash('success', 'Goals Published successfully');
        return redirect()->back();
    }else{
        Session::flash('info', 'You cannot proceed. Kindly Set all Goals before you publish!');
        return redirect()->back();
    } 
}
我得到了这个错误:

正在尝试获取非对象的属性“id”

这一行代码被突出显示:

$linemanageremails = DB::table('hr_employees')
    ->select('email')
    ->where('id', $linemanageruserids->id)
    ->first();
我发现错误是因为:$linemanageruserids为NULL


如何解决此问题?

您的
$linemanageruserids
返回空值,您可以有条件地进行检查并返回正确的响应

if($linemanageruserids)
        $linemanageremails = DB::table('hr_employees')->select('email')->where('id', $linemanageruserids->id)->first();
您还可以通过将返回对象强制转换为数组并获取计数来检查该对象的计数

    if(count((array)$linemanageruserids))
        $linemanageremails = DB::table('hr_employees')->select('email')->where('id', $linemanageruserids->id)->first();

您的
$linemanageruserids
返回null,您可以有条件地检查并返回正确的响应

if($linemanageruserids)
        $linemanageremails = DB::table('hr_employees')->select('email')->where('id', $linemanageruserids->id)->first();
您还可以通过将返回对象强制转换为数组并获取计数来检查该对象的计数

    if(count((array)$linemanageruserids))
        $linemanageremails = DB::table('hr_employees')->select('email')->where('id', $linemanageruserids->id)->first();

$linemanageremails
是否应为空?如果代码为空,我认为您不需要代码来工作。您应该在中放置一个
,如果为空则返回一般错误消息,而不是试图提供不存在的信息。是否应将
$linemanageremails
设置为空?如果代码为空,我认为您不需要代码来工作。您应该在
中输入一个
,如果为空则返回一般错误消息,而不是试图提供不存在的信息。