使编辑表单在多个选择框中显示选定的值,它会选择数据,但会使数据加倍

It select the option but it doubles the data doubles the data.

I tried but its not helping me.

{{ in_array( $rsmsl , $data['state'] ) ? 
    < option selected value = "{{ $rsms->statesId }}" > {{$rsms->statesName}} </option>                                                 :
    < option  value=" {{ $rsms->statesId }}" > {{ $rsms->statesName }} </option> }}

Controller Code

    public function editEmp($id){


            $task = DB::table('user')
                ->where('user.userId', '=', $id)
                ->select('user.*')
                ->get()[0];


            $states = DB::table('states')
                ->select('states.*')
                ->get()
                ->toArray();

            $role = DB::table('roll')
                ->select('roll.*')
                ->get();

            $sd = DB::table('sddetail')
                ->select('sddetail.*')
                ->get();

            $avp =   DB::table('avpdetail')
            ->select('avpdetail.*')
            ->get();

            $allData = array("user" => $task, "state" => $states, "role" => $role, "sd" => $sd, "avp" => $avp);


            return view('modal.editemp', ['data' => $allData]);

}

Blade Template for View the data.

    <?php
        $rsmex = explode(",",$data['user']->statesId);
    ?>


        <lable for="addStorename1" >Market Name</label>
            <select class="form-control my-select"  data-live-search="true" data-actions-box="true"  placeholder="Enter Role" name="Statename[]" multiple  >    
        @foreach($rsmex as $rsmsl)
                @foreach($data['state'] as $rsms)
                    <option @if ( $rsmsl == $rsms->statesId ) {{"selected"}} @endif value={{$rsms->statesId}}>{{$rsms->statesName}}</option>
                @endforeach
        @endforeach 
         </select>
   <?php
        $rsmex = explode(",",$data['user']->statesId);
    ?>
 @foreach($rsmex as $rsmsl)
   @foreach($data['state'] as $rsms)
     <option @if ( $rsmsl == $rsms->statesId ) {{"selected"}} @endif value={{$rsms->statesId}}>{{$rsms->statesName}}</option>
   @endforeach
@endforeach 

I'm not sure about the length of the $rsmex. But if the length of $rsmex is 2, it will loop through all the $data['state'] twice. Then you got those option data twice.

maybe this is what you want to achieve? Just to loop through the state, and see whether user has that statesID?

   @foreach($data['state'] as $rsms)
     <option @if (in_array($rsms->statesId, $rsmex)) {{"selected"}} @endif value={{$rsms->statesId}}>{{$rsms->statesName}}</option>
   @endforeach