Home > Blockchain >  How to fetch value of multiselect dropdown in Laravel 8?
How to fetch value of multiselect dropdown in Laravel 8?

Time:12-23

I have this code for multiselect dropdown

<select id="kit" name="tool_id[]" multiple >
    <option value="">Select Tool Name</option>
    <?php foreach($tools as $tool){?>
    <option value="<?php echo $tool->id;?>"><?php echo $tool->name;?></option>
    <?php }?>
</select>

I can insert data in database ,but unable to retrieve on show blade

enter image description here

value stores in database like this in array

Help me to retrieve array value from MySql DB

Retrieve array value from data base in laravel

CodePudding user response:

Since you are storing an array, you can use in_array to achieve the result you are looking for:

// I will assume here that you are passing a list of IDs from view
// I think in your example its "tool_id"

<option value="{{$tool->id}}" {{in_array($tool->id, $listOfIds) ? 'selected' : ''}}>{{$tool->name}}</option>

You can read more examples from https://www.w3schools.com/php/func_array_in_array.asp

Also you do not need to call <?php //code ?> in blade files, you can simply use {{ //code }} to render it, you can read more on blade syntax from https://laravel.com/docs/9.x/blade

Good luck!

CodePudding user response:

If you can fetch the value of tool_id from the database, use this.

{!! Form::select('tool_id', $yourOptionArray, $toolIds, ['class' => 'form-control', 'multiple' => 'multiple']) !!}

By default, this will add selected keywords to 11 and 13.

  • Related