victorybiz / laravel-simple-select

Laravel Simple Select inputs component for Blade and Livewire.
MIT License
121 stars 18 forks source link

How do I use the attribute "value"? #6

Closed ghost closed 2 years ago

ghost commented 2 years ago

I'm trying to use the select on my edit page, so far I manage to use multiple by passing an array of id to value.

controller.php

public function getSelected($role)
{
    $permissions = $role->permissions()->pluck('id');
    $array = [];

    foreach ($permissions as $value) {
        $array[] = $value;
    }

    return $array;
}

edit.blade.php

<x-simple-select
    name="permissions[]"
    id="permissions"
    multiple="multiple"
    :options="$permissions"
    value-field='id'
    text-field='name'
    :value="$selected"
    placeholder="Permissions"
    search-input-placeholder="Select Permissions..."
    :searchable="true"
/>

But the above code won't work in normal select, I try passing the id or the name directly but nothing would show.

<x-simple-select
    name="province"
    id="province"
    :options="$provinces"
    value-field='id'
    text-field='name'
    :value="$municipality->province_id"
    placeholder="Province"
    search-input-placeholder="Select Province..."
    :searchable="true"
/>

I'm still new to TALL so I don't understand anything when I read your code, wish you could help.

victorybiz commented 2 years ago

Can you please show an example of the data you passed to the :options prop.

From the sample you posted, your $provinces in :options should be array like the following with array keys specified in value-field and text-field props;

$provinces = [
    ['id' => '1', 'name' => 'Delta'],
    ['id' => '2', 'name' => 'Edo'],
    ['id' => '3', 'name' => 'Enugu'],
    ['id' => '4', 'name' => 'Lagos'],
];

and the selected value in $municipality->province_id should be a value of the province ID e.g 2 for forEdo`.

if you are using the simple-select in Livewire component, use wire:model instead of :value

ghost commented 2 years ago

this is how I fill the options. the columns are id and name;

$provinces = Province::orderBy('name')->get()->toArray();
ghost commented 2 years ago

I somehow found the problem, when I copy your example it work as you said but when I change the id from string to int it doesn't work anymore, since the id on my $provinces is int.

victorybiz commented 2 years ago

I just fixed this issue in v1.2.5, you can now use int as id.

ghost commented 2 years ago

Thank you! Working perfectly now.