Php ErrorException:未定义变量:Laravel控制器中的目标
在myPhp 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')
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');