[英]How to retrieve serialized data in Form::model and populate array of Form::select with multiselect in laravel
I have a serialized data saved in my database, it's a values from array of categories. 我有一个保存在我的数据库中的序列化数据,它是来自类别数组的值。 I created this because it's easy to manage what categories are selected. 我创建了这个,因为它很容易管理选择的类别。
So for creating a "page" i created create.blade.php page and form for multi select categories. 因此,为了创建“页面”,我为多选类别创建了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() }}
I defined $developer, $genre and $platform in my controller, this is basically a list of categories under specific id. 我在我的控制器中定义了$ developer,$ genre和$ platform,这基本上是特定id下的类别列表。
$platform = Category::->where('type', '=', 1)->lists('name', 'id');
So this returns an array of all categories for a view. 因此,这将返回视图的所有类别的数组。
----------------
| name | id |
----------------
| Windows | 1 |
----------------
| Linux | 2 |
----------------
| MacOS | 3 |
----------------
So this all works fine i have my select forms working fine with categories i specified and upon submit i get an array
of values, as array can't be saved i serialized that very simple with: 所以,这一切工作正常,我有我的工作的罚款与类别选择表格我指定并在提交我得到一个array
的值,因为阵列无法保存我认为序列化非常简单,:
serialize(Input::get('categories')
And i have serialized data in my database, i know someone would say it's not a good way if i need to search etc... i will not be able to use that field. 我已经在我的数据库中序列化数据,我知道有人会说如果我需要搜索等不是一个好方法...我将无法使用该字段。 But for me i need only to store id's of selected categories and query them all together at once. 但对我来说,我只需要存储所选类别的ID,并一次查询所有类别。 so this is good for me. 所以这对我有好处。
But now i have a problem, i am not finding a way to get that data when i edit a page. 但是现在我遇到了问题,我在编辑页面时找不到获取数据的方法。
I use Route::controller
so you know work flow how the pages are updated, stored, edited, created... And since form can automatically populate the fields using Form::model
i used that before and it's very nice feature. 我使用Route::controller
因此你知道工作流程如何更新,存储,编辑,创建页面......并且因为表单可以使用之前使用的Form::model
自动填充字段,这是非常好的功能。 But i don't know how to populate the fields now when i edit the page. 但是当我编辑页面时,我不知道如何填充字段。
Here is a form on my edit.blade.php 这是我的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() }}
So how do i get that serialized data and populate the select fields with selected categories when editing a page using Form::model
. 那么,在使用Form::model
编辑页面时,如何获取序列化数据并使用所选类别填充选择字段。 Any ideas, i searched on net and no answers. 任何想法,我在网上搜索,没有答案。
I think this should tell you what you need to know: Getting selected values from a multiple select form in Laravel 我认为这应该告诉您需要知道的事项: 从Laravel中的多选表单中获取所选值
So instead of passing in a null third parameter you instead pass in an array of selected values. 因此,您不是传入null的第三个参数,而是传入一个选定值的数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.