Firstly, according to the documentation, isset()
expects a variable. It can't evaluate expressions, as you are trying with isset($request['active_year'] == "1")
. Secondly, this is more related to PHP itself than Laravel.
You should split the checking into two parts: first, check if the variable $request['active_year']
exists. You can do that using isset($request['active_year'])
or with !!$request['active_year']
(using double-not operator). Then, check whether the value is equal to 1 with $request['active_year'] == "1"
. Note that using ==
is a loose comparison. If you wish to also check if the variable is of type string, you should use ===
which is a strict comparison.
Considering the above, I would write:
if(!!$request['active_year'] && $request['active_year'] === "1") {
$activeYear = 1;
$currentActiveYear = AcademicYear::where('active_year', 1)->update(['active_year' => 0]);
} else {
$activeYear = 0;
}