skip to Main Content

I have two array $softwarelist and $virt_software_select when i use @foreach in blade template like below code i got checked but $softwarelist is double in label.

$softwarelist =["Adobe Photoshop","Adobe Illustrator","Corel Draw","My SQL","Oracle"];
$virt_software_select=["Adobe Photoshop","My SQL"];

@foreach($softwarelist as $slst)
    @foreach($virt_software_select as $sw_usr)
        @if($slst->software==$sw_usr->virtualize_software)
            <label>
                <input type="checkbox" checked name="virt_software[]" value="{{$slst->software}}"/> 
                  {{$slst->software}}
            </label>
        @else
            <label>
                <input type="checkbox" name="virt_software[]" value="{{$slst->software}}"/> 
                  {{$slst->software}}
            </label> 
        @endif
    @endforeach
@endforeach

i got these
enter image description here

How can i get checked the same data in two array but not double data with $softwarelist in laravel?

2

Answers


  1. You have to use in_array() php function then it will work and use only one foreach

    $softwarelist =["Adobe Photoshop","Adobe Illustrator","Corel Draw","My SQL","Oracle"];
    $virt_software_select=["Adobe Photoshop","My SQL"];
    
    @foreach($softwarelist as $slst)
        @if(in_array($slst,$virt_software_select))
            <?php $checked ="checked"; ?>
        @else
            <?php $checked =""; ?>
        @endif
            <label>
                <input type="checkbox" {{$checked}} name="virt_software[]" value="{{$slst->software}}"/> 
                  {{$slst->software}}
            </label>
    @endforeach
    

    Hope it helps!

    Login or Signup to reply.
  2. You have to use in_array() function instead of using nested foreach

    $softwarelist =["Adobe Photoshop","Adobe Illustrator","Corel Draw","My SQL","Oracle"];
    $virt_software_select=["Adobe Photoshop","My SQL"];
    
    @foreach($softwarelist as $slst)
        <label>
        @if(in_array($slst,$virt_software_select))
             <input type="checkbox" checked name="virt_software[]" value="{{$slst->software}}"/>{{$slst->software}}
        @else
             <input type="checkbox" name="virt_software[]" value="{{$slst->software}}"/>{{$slst->software}}
        @endif
        </label>
    @endforeach
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search