如何在模型绑定中使用带有 Laravel 的复选框组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26163502/
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 a checkbox group with Laravel's from model binding
提问by Clayton Leis
I'm trying to save a checkbox group as a serialized string in my main model's database table. AudienceGroups is simply a property of my main eloquent model. If I simply do
我正在尝试将复选框组保存为主模型数据库表中的序列化字符串。AudienceGroups 只是我的主要雄辩模型的一个属性。如果我只是这样做
{{Form::checkbox('AudienceGroups[]', 'preschool');}}
{{Form::checkbox('AudienceGroups[]', 'elementary');}}
with no logic in my model, then the database column is set as the string Array
. If I drop the brackets and just use {{Form::checkbox('AudienceGroups', 'preschool');}}
then the column is set to the value of the last checkbox, in my case "seniors".
在我的模型中没有逻辑,那么数据库列被设置为 string Array
。如果我去掉括号并直接使用,{{Form::checkbox('AudienceGroups', 'preschool');}}
那么该列将设置为最后一个复选框的值,在我的例子中是“seniors”。
If I serialize the array before saving the instance of my model, it saves as a serialized array like it's supposed to, but I'm having trouble getting it to repopulate the proper checkboxes on page load (from old input or from the the saved model).
如果我在保存模型实例之前序列化数组,它会像预期的那样保存为序列化数组,但是我无法让它在页面加载时重新填充正确的复选框(从旧输入或从保存的模型)。
How should I attack this? Serialize the array myself and then use my own logic to control the third parameter to Form::checkbox() which controls the checked status? If so, can someone help me with that bit. I attempted it with Input::old and in_array but could never get it to work.
我应该如何攻击这个?自己序列化数组,然后使用我自己的逻辑来控制控制选中状态的 Form::checkbox() 的第三个参数?如果是这样,有人可以帮我解决这个问题。我用 Input::old 和 in_array 尝试过,但永远无法让它工作。
Thanks
谢谢
回答by Marcin Nabia?ek
First, it's not a best solution to put properties serialized in one column. You should not do that because if you want to search something in future in DB for this data you won't be able to do that.
首先,将属性序列化在一列中并不是最好的解决方案。您不应该这样做,因为如果您想在将来在 DB 中搜索此数据的某些内容,您将无法这样做。
Solution for your problem is not complicated I think.
我认为您的问题的解决方案并不复杂。
I've prepared 2 simple routes:
我准备了两条简单的路线:
Route::get('/checkbox',function() {
// normally you would get it from model and unserialize it
$fromModel = ['preschool','elementary','something'];
return View::make('form')->with('fromModel',$fromModel);
});
Route::post('/checkbox-post',function() {
$agroups = Input::get('AudienceGroups');
foreach ($agroups as $item) {
echo $item."<br />";
}
$toModel = serialize($agroups);
echo $toModel;
// now you insert it into model
});
and form:
并形成:
{{ Form::open(['url' => 'checkbox-post', 'role' => 'form']) }}
Preschool:
{{Form::checkbox('AudienceGroups[]', 'preschool', (in_array('preschool', $fromModel) ? true : false))}}<br />
Elementary:
{{Form::checkbox('AudienceGroups[]', 'elementary', (in_array('elementary', $fromModel) ? true : false))}}<br />
Something else:
{{Form::checkbox('AudienceGroups[]', 'somethingelse', (in_array('somethingelse', $fromModel) ? true : false))}}<br />
Something:
{{Form::checkbox('AudienceGroups[]', 'something', (in_array('something', $fromModel) ? true : false))}}<br />
{{Form::submit('Send')}}
{{ Form::close() }}
You can run it using http://localhost/yourproject/checkbox
- form is initalized with 3 checkbox checked. In blade you can use in_array
to check correct checkboxes and in POST route you can simple use this Input::get('AudienceGroups')
as array and serialize it.
您可以使用http://localhost/yourproject/checkbox
- 表单初始化并选中 3 个复选框来运行它。在刀片中,您可以in_array
用来检查正确的复选框,在 POST 路由中,您可以简单地将其Input::get('AudienceGroups')
用作数组并将其序列化。
回答by Clayton Leis
Update
更新
Looks like the html helper was removed from the core repo without this ever being fixed. https://github.com/laravel/framework/issues/5078
看起来 html 帮助程序已从核心存储库中删除,而此问题从未得到修复。https://github.com/laravel/framework/issues/5078
The problem I was running into was that the third parameter of Form::checkbox() doesn't function as expected when form-model binding is used.
我遇到的问题是 Form::checkbox() 的第三个参数在使用表单模型绑定时没有按预期运行。
The laravel documentationstates that the value priority is:
该laravel文档指出值的优先级是:
- Session Flash Data (Old Input)
- Explicitly Passed Value
- Model Attribute Data
- 会话闪存数据(旧输入)
- 显式传递的值
- 模型属性数据
However, if you look at getCheckboxCheckedStatus() in /Illuminate/Html/FormBuilder.php, you'll see that the priority for checked status is actually:
但是,如果您查看/Illuminate/Html/FormBuilder.php中的getCheckboxCheckedStatus() ,您会发现已检查状态的优先级实际上是:
- Old Input
- Model Data
Explicitly Passed Value
protected function getCheckboxCheckedState($name, $value, $checked) { if (isset($this->session) && !$this->oldInputIsEmpty() && is_null($this->old($name)) ) { return false; } if ($this->missingOldAndModel($name)) { return $checked; } $posted = $this->getValueAttribute($name); return is_array($posted) ? in_array($value, $posted) : (bool)$posted; }
- 旧输入
- 模型数据
显式传递的值
protected function getCheckboxCheckedState($name, $value, $checked) { if (isset($this->session) && !$this->oldInputIsEmpty() && is_null($this->old($name)) ) { return false; } if ($this->missingOldAndModel($name)) { return $checked; } $posted = $this->getValueAttribute($name); return is_array($posted) ? in_array($value, $posted) : (bool)$posted; }
While I was in the code I also saw that Laravel has in_array built in provided that the parameter is an array. So I unserialized $myModelInstance->AudienceGroups
right before calling View::make() in my controller.
当我在代码中时,我还看到 Laravel 内置了 in_array ,前提是参数是一个数组。所以我$myModelInstance->AudienceGroups
在我的控制器中调用 View::make() 之前反序列化。