我有一个保存在我的数据库中的序列化数据,它是来自类别数组的值。我创建了这个,因为它很容易管理选择的类别。
所以创建一个"页面"我为多选类别创建了create.blade.php页面和表单。
{{ Form::open(['role' => 'form', 'method' => 'post', 'route' => 'admin.games.store', 'files' => true]) }}
{{ Form::select('categories[1][]', $platform, null, ['multiple'=>true, 'class' => 'form-control']) }}
{{ Form::select('categories[2][]', $genre, null, ['multiple'=>true, 'class' => 'form-control']) }}
{{ Form::select('categories[3][]', $developer, null, ['multiple'=>true, 'class' => 'form-control']) }}
{{ Form::close() }}
我在我的控制器中定义了$ developer,$ genre和$ platform,这基本上是特定id下的类别列表。
$platform = Category::->where('type', '=', 1)->lists('name', 'id');
因此,这将返回视图的所有类别的数组。
----------------
| name | id |
----------------
| Windows | 1 |
----------------
| Linux | 2 |
----------------
| MacOS | 3 |
----------------
所以这一切都运行正常,我的选择表单与我指定的类别一起工作正常,提交后我得到一个array
值,因为数组无法保存,序列化非常简单:< / p>
serialize(Input::get('categories')
我已经在我的数据库中序列化了数据,我知道如果我需要搜索等,有人会说这不是一个好方法...我将无法使用该字段。但对我来说,我只需要存储所选类别的ID并一次查询所有类别。所以这对我有好处。
但是现在我遇到了问题,我在编辑页面时找不到获取数据的方法。
我使用Route::controller
,因此您了解工作流程如何更新,存储,编辑和创建页面......并且由于表单可以使用Form::model
自动填充字段,我之前使用过它&#&# 39;非常好的功能。但是,当我编辑页面时,我现在不知道如何填充字段。
这是我的edit.blade.php上的表格
{{ Form::model($game, ['role' => 'form', 'method' => 'PUT', 'route' => ['admin.games.update', $game->id ], 'files' => true]) }}
{{ Form::select('categories[1][]', $platform, null, ['multiple'=>true, 'class' => 'form-control']) }}
{{ Form::select('categories[2][]', $genre, null, ['multiple'=>true, 'class' => 'form-control']) }}
{{ Form::select('categories[3][]', $developer, null, ['multiple'=>true, 'class' => 'form-control']) }}
{{ Form::close() }}
那么在使用Form::model
编辑页面时,如何获取序列化数据并使用所选类别填充选择字段。任何想法,我在网上搜索,没有答案。
答案 0 :(得分:0)
我认为这应该告诉你需要知道的事情:Getting selected values from a multiple select form in Laravel
因此,您不是传入null的第三个参数,而是传入一个选定值的数组。