php Laravel 5.4 中的单选按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42318180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:35:03  来源:igfitidea点击:

Radio button in Laravel 5.4

phplaravellaravel-5.3laravel-5.4

提问by Ovidiu G

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.

这是我想这样做的方式:我在用户表中添加了一个布尔字段,我想检查是否选中了我要购买,然后我将 1 存储在数据库中,如果另一个被选中,则我存储 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...

问题是,如果单选按钮被选中,我不知道如何检查控制器。 我试图查看文档和谷歌,但我发现的只是一些使用 Form 外观的想法在 5.4 中不再使用...

采纳答案by Qazi

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);
}

回答by Pramod Patil

HTML

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
  }