0

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.

lonerunner
  • 1,282
  • 6
  • 31
  • 70

1 Answers1

0

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.

Community
  • 1
  • 1
delatbabel
  • 3,601
  • 24
  • 29
  • Well you are not quite right but i will investigate further to see about multiple selects. When you use `Form::model` instead `Form::open` than `Form::model` automatically populates that field, but i must set it to null just because i have fourth argument for multiple and class. In previous forms i built i didn't used multiple arrays for select and it worked even if third param is null because `Form::model` fills up that. – lonerunner Feb 13 '15 at 22:17
  • Ah, I see what your problem is. I don't think that you can do this using Form::model unless you write a custom populator for your select object. Form::model is expecting to find the data in the model object, and the array of categories won't be there (instead it's a serialized array). Perhaps it's probably easier in this case just to use Form::open and populate the form data manually after unserializing the array. – delatbabel Feb 14 '15 at 02:22