How to use a normal if statement in PHP in controller to check if a dropdown has a selected value
Hi there, I hope I can get some help. I am new to Laravel and PHP and loving it more daily. I have a small issue, how can I write an IF statement to check if a dropdown has a selected value? If it does, code must execute in a way else in a different way.
Here is my controller (i want something in the if like this between ??):
public function runCommand(Request $request){
$user = User::select("id", "email")->first();
$signature = $request->input('signature');
if (??dropdown is selected??){
$command = Artisan::call($signature, ['user' => $user->email]);
}else{
$command = Artisan::call($signature);
}
return response($command);
}
$selectedUser = $request->user;
if ( ! empty($selectedUser)) {
// a user was selected
} else {
// no user was selected
}
Side Note: I'd remove $user = User::select("id", "email")->first(); from where you have it and move it into the if() block for if a user was selected. You are executing the query whether a user was selected or not, and there is no need to run the query if a user wasn't selected since it's not used in that case. It's an unnecessary hit to the database.