Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

horcrux88's avatar

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);
}
0 likes
3 replies
rob897's avatar

you just need the name of your dropdown and then can check if it is set or not.

if(isset($request->downDownFieldName)) {

}
ejdelmonico's avatar

Each select option should have a value. Just check if the select's chosen value is equal to the one you are filtering.

Cronix's avatar

I usually have a default option that has no value

<select name="user">
    <option value="" selected>Select a User</option>
    <option value="4">Bill</option>
    <option value="13">Jen</option>
</select>

Then in controller:

$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.

Please or to participate in this conversation.