Question

I am using this multiselect dropdown plugin . I can get all the ids of the selected items in the dropdown during the store method. However during the edit method when ever i am trying to load the entity that has multiple values , i am unable to mark the items as checked in the dropdown.

so for example - Suppose I am working with Contacts. Each contact can belong to many categories. There is a belongsToMany relationship between the contacts and the categories. Whenever I am adding a new contact (and if the user has selected many categories) i get the id of all the categories and assign it to the contact. Now when I am trying to load the contact again, i have to display the list of categories that were selected for this contact - which i have ben unable to do so till now.

Was it helpful?

Solution 2

Here's how I accomplish multi-selects in Laravel 4:

<?= Form::select(
    'category_ids[]',
    App::make('Category')->lists('name', 'id'),
    $contact->categories()->select('categories.id AS id')->lists('id'),
    [
        'class' => 'form-control',
        'multiple'
    ]
)?>

The resulting select markup looks like this:

<select class="form-control" multiple="multiple" name="category_ids[]">
    <option value="1" selected="selected">category 1</option>
    <option value="2">category 2</option>
</select>

And then, when you update, you'll need to add this line after validating your model:

$contact->categories()->sync(Input::get('category_ids'));

Use this in both your create and edit forms. In your create action, $contact->categories() will be empty, so the select will not be populated, but in the edit action, you will get the properly selected values.

Edit: In order to share the form like this, you'll need to pass in a new instance of the contact model in your create action like so:

public function create()
{
    $contact = App::make('Contact');
    return View::make('contact.create', concat('contact'));
}

In your shared form, $contact will always be available even if it's not yet persisted.

OTHER TIPS

Travis answers really helped me a lot. Hence I am marking this as the correct answer. However there were some updates that I had to do . Following is what I had to do ..

            @if(isset($contact))
            <?= Form::select(
                'category_ids[]',
                Category::lists("name", "id"),
                $contact->categories()->select('categories.id AS id')->lists('id'),
                [
                    'class' => 'form-control multiselect',
                    'multiple'
                ]
            )?>
        @else
            {{ Form::select("category_ids[]", Category::lists("name", "id"), Input::old("category_id"), array( "class" => "form-control multiselect" , "multiple" => "multiple" )) }}
        @endif

I am using the same form for the create and edit operations , so in the create form , it was throwing me an error on the contact->categories line which is true because in the create method the contact is null. Hence the check.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top