skip to Main Content

I have this code for multiselect dropdown

<select id="kit" name="tool_id[]" multiple class="form-control single-select selectpicker">
    <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

3

Answers


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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. As the value of the select tag is being saved as an array, this can be simply done with the in_array condition as mentioned below:

    <option value="{{ $tool->id }}" @if(in_array($tool->id,$IDsList)) selected @endif>{{ $tool->name }}</option>
    

    Assuming the $IDsList (passed to the view blade from controller) have the value stored for the $tool->id

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search