如何获取Laravel 4控制器中的一系列复选框的值(如果选中) [英] How to get the values for a series of checkboxes in Laravel 4 controller (if checked)
问题描述
我想获取我在Laravel 4表单中设置的一系列复选框的值。下面是视图中的代码设置复选框:
@foreach($ friends为$ friend)
; input tabindex =1type =checkboxname =friend []id ={{$ friend}}value ={{$ friend}}>
@endforeach
在我的控制器中,我想获取复选框的值并将它们放在数组中。我不知道如何做到这一点,但我认为是这样的:
array [];
foreach($ friend as $ x)
if(isset(Input :: get('friend')){
array [] = Input :: get ');
}
endforeach
编辑:
这是我在控制器中的:
public function describe_favorite(){
$ fan = user() - > id);
$ fan-> favorite_venue = Input :: get('venue');
$ fan-> favorite_experience = Input :: get ;
$ friends_checked = Input :: get('friend []');
print_r($ friends_checked);
if(is_array $ friends_checked))
{
$ fan-> experience_friends = 5;
}
$ fan-> save();
return Redirect :: to('fans / home');
}
它不是通过if循环。如何查看print_r的输出以查看$ friends_checked变量中的内容?
如果复选框相关,则应在name属性中使用[]。
@foreach($ friends as $ friend)
< input tabindex =1type =checkboxname =friend []id = {{$ friend}}value ={{$ friend}}>
@endforeach
$ friends_checked = Input :: get('friend');
if(is_array($ friends_checked))
{
//与选中的朋友做朋友
}
I would like to get the values for a series of checkboxes I have set up in a Laravel 4 form. Here is the code in the view setting up the checkboxes:
@foreach ($friends as $friend)
<input tabindex="1" type="checkbox" name="friend[]" id="{{$friend}}" value="{{$friend}}">
@endforeach
In my controller, I would like to get the values for the checked boxes and put them in an array. I am not exactly sure how to do this, but I assume it is something like:
array[];
foreach($friend as $x)
if (isset(Input::get('friend')) {
array[] = Input::get('friend');
}
endforeach
Could you provide me with a solution to do this? Thank you.
EDIT:
This is what I have in the controller:
public function describe_favorite() {
$fan = Fan::find(Auth::user()->id);
$fan->favorite_venue = Input::get('venue');
$fan->favorite_experience = Input::get('experience');
$friends_checked = Input::get('friend[]');
print_r($friends_checked);
if(is_array($friends_checked))
{
$fan->experience_friends = 5;
}
$fan->save();
return Redirect::to('fans/home');
}
It is not going through the "if" loop. How do I see the output of the print_r to see what's in the $friends_checked variable?
If checkboxes are related then you should use [] in the name attribute.
@foreach ($friends as $friend)
<input tabindex="1" type="checkbox" name="friend[]" id="{{$friend}}" value="{{$friend}}">
@endforeach
$friends_checked = Input::get('friend');
if(is_array($friends_checked))
{
// do stuff with checked friends
}
这篇关于如何获取Laravel 4控制器中的一系列复选框的值(如果选中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!