Radio button in Laravel 5.4

52,432

Solution 1

Remeber: Checkbox and radio button sends values on server end, if and only if they are marked as checked, if not checked that, then no values will be sent out to controller end. so you can do this check in your controller

eg:

public function myMethod(Request $request){
  //2nd parameter means, if radio is not selected then use default value
  $radio = $request->get('radion_button', 0);
}

Solution 2

HTML

  {{ Form::radio('result', 'buy' , true) }}
  {{ Form::radio('result', 'sell' , false) }}

Without Form Facade

  <input type="radio" name="result" value="buy" checked>
  <input type="radio" name="result" value="sell">

Controller

  $fields = Input::get('result');
  if($fields == 'buy'){
  // logic
  }
  else{
  // logic
  } 
Share:
52,432
Ovidiu G
Author by

Ovidiu G

Updated on July 05, 2022

Comments

  • Ovidiu G
    Ovidiu G almost 2 years

    I have a register form where users can sign up and I recently added two radio buttons to define users type.

    Example:

    (*) I want to buy   (*) I want to sell
    

    Here's how I was thinking to do this: I added a boolean field in the users table and I was thinking to check if I want to buy is checked then I store 1 in the database if the other one is checked then I store 0.

    The thing is, I'm not sure how to check in the controller if the radio button is selected or not.. I tried to look in the documentation and on google but all I found were some ideas using the Form facade which as I know is no longer used in 5.4...