如何在laravel的multiselect复选框列表中显示选中的复选框?

如何在laravel的multiselect复选框列表中显示选中的复选框?,laravel,multi-select,multichoiceitems,jquery-ui-multiselect,Laravel,Multi Select,Multichoiceitems,Jquery Ui Multiselect,我有一个多选复选框列表。我想使用选中的复选框在列表中显示存储的值 用户信息存储在Partner_Prefence表和名为p_religion的用户宗教列中 从宗教表中获取宗教 $religion_data=DB::table('religion')->select('religion_id','religion_name')->orderby('religion_name')->get(); 多选名单 <select multiple="multiple" name=

我有一个多选复选框列表。我想使用选中的复选框在列表中显示存储的值

用户信息存储在Partner_Prefence表和名为p_religion的用户宗教列中

从宗教表中获取宗教

$religion_data=DB::table('religion')->select('religion_id','religion_name')->orderby('religion_name')->get();
多选名单

<select  multiple="multiple" name="religion[]">
   @foreach($religion_data as $religion)
     <option value="{{$religion->religion_id}}" {{$profile_data->p_religion == $religion->religion_id  ? 'selected' : ''}}>{{$religion->religion_name}}</option>
   @endforeach
</select>

如果p_宗教列是多选列表,它是否保存多个ID?使用in_数组而不是使用$profile_data->p_religation==$religure->religation\u id是否有效

在\u数组$lignity->lignity\u id中,分解“,”,$profile\u data->p\u lignity

在存储内爆阵列时添加了爆炸调用

您还可以尝试对if语句内联使用blade语法,以查看其显示是否不同

<select  multiple="multiple" name="religion[]">
     @foreach($religion_data as $religion)
          <option value="{{$religion->religion_id}}" @if($profile_data->p_religion == $religion->religion_id) selected @endif>
               {{$religion->religion_name}}
          </option>
     @endforeach
</select>

据我所知,您有多选表单,因此您需要显示所选多列

您将ID存储为字符串,但很难检查字符串中的特定数字。如果将字符串转换为数组,则可以使用in_数组方法轻松地进行检查。如果给定数组中存在给定值,则此方法将返回true

<select multiple="multiple" name="religion[]">
    {{-- no need to explode every time, it will reduce your performance --}}
    @php($religions = explode(',', $profile_data->p_religion))
    @foreach($religion_data as $religion)
        <option
                value="{{$religion->religion_id}}"
                {{--if user religion id exist in religions then mark as selected--}}
                {{in_array($religion->religion_id,$religions) ? "selected" : ""}}>
            {{$religion->religion_name}}
        </option>
    @endforeach
</select>

您能告诉我们您如何存储所选的宗教id吗?请使用dd方法转储并向我们显示个人资料_数据。示例dd$profile_data@HasanTıngır$宗教=$请求->输入“宗教”$宗教=内爆“,”,$宗教;DB::表'partner_prefence'->插入['p_婚姻状况'=>$关系状况,'p_宗教'=>$宗教];如何在刀片语法中写入数组$LITIONG->LITIONG\U id,explode',',$profile\U data->p\U LITIONG。您可以将其用刀片大括号括起来。它实际上只是替换if语句的==部分。{{$religation->religation_id,explode','profile_data->p_religation?'selected':}
<select  multiple="multiple" name="religion[]">
     @foreach($religion_data as $religion)
          <option value="{{$religion->religion_id}}" @if($profile_data->p_religion == $religion->religion_id) selected @endif>
               {{$religion->religion_name}}
          </option>
     @endforeach
</select>
<select multiple="multiple" name="religion[]">
    {{-- no need to explode every time, it will reduce your performance --}}
    @php($religions = explode(',', $profile_data->p_religion))
    @foreach($religion_data as $religion)
        <option
                value="{{$religion->religion_id}}"
                {{--if user religion id exist in religions then mark as selected--}}
                {{in_array($religion->religion_id,$religions) ? "selected" : ""}}>
            {{$religion->religion_name}}
        </option>
    @endforeach
</select>