如何在 Laravel 中使用单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41696945/
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
How to use radio button with laravel
提问by Naj
I am trying to learn laravel 5, so I strated by creating some simple forms. My form contains (id, title,..) and radio button
我正在尝试学习 Laravel 5,所以我通过创建一些简单的表单来进行策略。我的表单包含 (id, title,..) 和单选按钮
<label for="importance">Importance</label>
<input type="radio" name="Importance"
<?php if (isset($importance) && $importance=="tres_important") echo "checked";?>
value="tres_important">très important
<input type="radio" name="importance"
<?php if (isset($importance) && $importance=="important") echo "checked";?>
value="important">important
Now, I need to know what I should add to my migration file, to make it work "create_projets_table"
现在,我需要知道我应该在迁移文件中添加什么,以使其工作“create_projets_table”
public function up()
{
Schema::create('projets', function (Blueprint $table) {
$table->increments('id',true);
$table->string('title');
$table->date('dateDebut');
$table->timestamps();
});
}
Thank you in advance
先感谢您
UPDATES :
更新 :
Based on the comments bellow, I made some changes, but still not working, it seems like something went wrong with "submit.blade.php"
根据下面的评论,我做了一些更改,但仍然无法正常工作,“submit.blade.php”似乎出了点问题
submit.blade.php :
提交.blade.php :
<div class="form-group">
<label class="radio-inline">
<input type="radio" id="tres_important" name="importance" value="tres_important">Très important</label>
<label class="radio-inline">
<input type="radio" id="important" name="importance" value="important">Important</label>
</div>
and this is my migration "create_projets_table.php" :
这是我的迁移“create_projets_table.php”:
public function up()
{
Schema::create('projets', function (Blueprint $table) {
$table->increments('id',true);
$table->string('title');
$table->date('dateDebut');
$table->date('dateFin');
$table->float('cout');
$table->integer('importance');
$table->timestamps();
});
}
This route.php :
这个 route.php :
Route::post('/submit', function(Request $request) {
$validator = Validator::make($request->all(), [
'title' => 'required|max:255',
'annee_realisation' => 'required|max:255',
'cout' => 'required|max:255',
'importance' => 'required',
]);
if ($validator->fails()) {
return back()
->withInput()
->withErrors($validator);
}
$projet = new \App\Projet;
$projet->title = $request->title;
$projet->annee_realisation = $request->annee_realisation;
$projet->cout = $request->cout;
$projet->importance = $request->importance;
$projet->save();
return redirect('/');
});
And this my controller "Project_Controller" :
这是我的控制器“Project_Controller”:
class Project_Controller extends Controller
{
//
$this->validate(request(), [
'importance' => 'required'
]);
}
I got the following error :
我收到以下错误:
Undefined variable : importance
未定义变量:重要性
回答by Manjeet Barnala
You can use laravel form methods, like below
您可以使用 Laravel 表单方法,如下所示
Form::radio('name', 'value', true);
Example:
例子:
<label class="radio inline">
{!! Form ::radio('importance','tres_important',($importance == 'tres_important' ? true : false)) !!}
Très important
</label>
<label class="radio inline">
{!! Form ::radio('importance','important',($importance == 'important' ? true : false)) !!}
Important
</label>
Read Documentation https://laravelcollective.com/docs/5.0/html#checkboxes-and-radio-buttons
阅读文档https://laravelcollective.com/docs/5.0/html#checkboxes-and-radio-buttons
回答by ATechGuy
I don't see anywhere in your code you have set the value of "$importance", can you show the code where you set that value?.
我在您的代码中没有看到您设置了“$importance”的值,您能显示设置该值的代码吗?。
Your checking to see if its set which is fine, but then also checking its value.
您检查它的设置是否正常,然后还要检查它的值。
if (isset($importance) && $importance=="tres_important"
if it is not set you cant chcek it to see what the value is, which is what it looks like your trying to do in the code i can see. Thats where your undefined error is coming from. If you can post full code it will be easier to track down
如果未设置它,您将无法检查它以查看值是什么,这就是您在我可以看到的代码中尝试执行的操作。那就是您未定义错误的来源。如果您可以发布完整代码,则更容易追踪