简体   繁体   中英

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.

{{ 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.

$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:

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. 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. 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

{{ 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 . 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

So instead of passing in a null third parameter you instead pass in an array of selected values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM