![](/img/trans.png)
[英]How to populate Form::select() with JSON specified data in Laravel 5.5
[英]How to retrieve serialized data in Form::model and populate array of Form::select with multiselect in laravel
我有一個保存在我的數據庫中的序列化數據,它是來自類別數組的值。 我創建了這個,因為它很容易管理選擇的類別。
因此,為了創建“頁面”,我為多選類別創建了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
的值,因為陣列無法保存我認為序列化非常簡單,:
serialize(Input::get('categories')
我已經在我的數據庫中序列化數據,我知道有人會說如果我需要搜索等不是一個好方法...我將無法使用該字段。 但對我來說,我只需要存儲所選類別的ID,並一次查詢所有類別。 所以這對我有好處。
但是現在我遇到了問題,我在編輯頁面時找不到獲取數據的方法。
我使用Route::controller
因此你知道工作流程如何更新,存儲,編輯,創建頁面......並且因為表單可以使用之前使用的Form::model
自動填充字段,這是非常好的功能。 但是當我編輯頁面時,我不知道如何填充字段。
這是我的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
編輯頁面時,如何獲取序列化數據並使用所選類別填充選擇字段。 任何想法,我在網上搜索,沒有答案。
我認為這應該告訴您需要知道的事項: 從Laravel中的多選表單中獲取所選值
因此,您不是傳入null的第三個參數,而是傳入一個選定值的數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.