Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php ErrorException:未定义变量:Laravel控制器中的目标_Php_Laravel - Fatal编程技术网

Php ErrorException:未定义变量:Laravel控制器中的目标

Php ErrorException:未定义变量:Laravel控制器中的目标,php,laravel,Php,Laravel,在myLaravel-5.8控制器中,我有以下代码: public function selfReview() { try { $userCompany = Auth::user()->company_id; $userEmployee = Auth::user()->employee_id; $identities = DB::table('appraisal_identity')->select('id')

在my
Laravel-5.8控制器中,我有以下代码:

public function selfReview()
{
    try {  
        $userCompany = Auth::user()->company_id;
        $userEmployee = Auth::user()->employee_id;

        $identities = DB::table('appraisal_identity')->select('id')
                        ->where('company_id', $userCompany)
                        ->where('is_current', 1)
                        ->first();              
        
        if (Auth::user()->hasRole('Employee')) {
            if(empty($identities)) {
                Session::flash('error', 'The current performance year has not been setup.');

                return redirect()->back();
            } else {
                $goals = Goal::where('employee_id', $userEmployee)
                            ->where('appraisal_identity_id', $identities->id)->where('is_approved', 3)
                            ->where('is_visible', 1)
                            ->where('is_special_project', 0)
                            ->whereNull('deleted_at')
                            ->get();
            } 
                       
            $incompleteCount = $goals->filter(function($item) {
                return ($item->line_manager_mid_year_approved == 0 || $item->line_manager_mid_year_approved == 2)
            })->count();                                        
        }   
        
        return view('selfReview')->with(['goals' => $goals, 'incompleteCount' => $incompleteCount]); 
    } catch (Exception $exception) {
        Log::error($exception);
        Session::flash('error', 'Action failed! Please try again');

        return back();
    }                     
}
当我想要渲染刀片时,我遇到了以下错误:

production.ERROR: ErrorException: Undefined variable: goals
它指向这条线:

->with(['goals' => $goals, 'incompleteCount' => $incompleteCount]); 
我如何解决它


谢谢

您在if-else中声明了
$goals
,如果
为空($identies)
通过了第一个if条件,则不会声明
$goals
。因此,您应该在if else之前申报他:

$goals = []

if (Auth::user()->hasRole('Employee')) {
if(empty($identities)){
    Session::flash('error', 'The current performance year has not been setup.');
    return redirect()->back();
}else{
   $goals = Goal::where('employee_id', $userEmployee)->where('appraisal_identity_id', $identities->id)->where('is_approved', 3)->where('is_visible', 1)->where('is_special_project', 0)->whereNull('deleted_at')->get();
}                        
    $incompleteCount = $goals->filter(function($item) {
           return ($item->line_manager_mid_year_approved == 0 || $item->line_manager_mid_year_approved == 2);
      })->count();                                        
}  

return view('selfReview')
            ->with(['goals' => $goals, 'incompleteCount' => $incompleteCount]); 
} catch (Exception $exception) {
    Log::error($exception);
    Session::flash('error', 'Action failed! Please try again');
    return back();
    }                     
} 另外,尝试使用
compact()
方法,而不是
with()

return view('selfReview', compact('goals', 'incompleteCount');