我的数据库中保存了一个序列化的数据,它是类别数组中的值。我创建这个是因为它很容易管理选择的类别。
因此,为了创建一个"页面",我为多选类别创建了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、$type和$platform,这基本上是特定id下的类别列表。
$platform = Category::->where('type', '=', 1)->lists('name', 'id');
因此,这将返回一个视图的所有类别的数组。
----------------
| name | id |
----------------
| Windows | 1 |
----------------
| Linux | 2 |
----------------
| MacOS | 3 |
----------------
所以这一切都很好,我让我选择的表单可以很好地处理我指定的类别,提交后我得到了一个值的array
,因为数组无法保存,所以我用序列化了它
serialize(Input::get('categories')
我已经在我的数据库中序列化了数据,我知道有人会说,如果我需要搜索等,这不是一个好方法……我将无法使用该字段。但对我来说,我只需要存储所选类别的id,并一次将它们全部查询在一起。所以这对我有好处。
但现在我遇到了一个问题,当我编辑页面时,我找不到获取数据的方法。
我使用Route::controller
,这样你就知道页面是如何更新、存储、编辑、创建的。。。由于表单可以使用Form::model
自动填充字段,我以前使用过它,这是一个非常好的功能。但我现在不知道在编辑页面时如何填充字段。
这是我编辑的.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
编辑页面时,我如何获得序列化的数据并用选定的类别填充选择字段呢。任何想法,我在网上搜索,没有答案。
我认为这应该告诉你需要知道的:在Laravel 中从多选表单中获取选定的值
因此,您不需要传入第三个空参数,而是传入一个选定值的数组。