4

I have a multi-select checkbox list. I want to show stored value in list using checkbox selected.

User informations are stored in Partner_Prefence table and user religion column named as p_religion

$profile_data= DB::table('partner_prefence')->select('p_religion')->first();

Fetching religions from religions table

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

Multiselect list

<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>

I'm having trouble with showing which religions user have

{{$profile_data->p_religion == $religion->religion_id  ? 'selected' : ''}}
3
  • can you show us how you store selected religions id ? Commented Oct 1, 2018 at 18:28
  • also please dump and show us profile_data with dd method. example dd($profile_data) Commented Oct 1, 2018 at 18:29
  • @HasanTıngır $religion = $request->input('religion'); $religion = implode(',', $religion); DB::table('partner_prefence')->insert(['p_marital_status' => $relationship_status, 'p_religion' => $religion]); Commented Oct 2, 2018 at 4:22

2 Answers 2

2

as I understand you have multi select form, so you need show selected multiple column..

You're storing ids as a string but it's hard check that certain number in string. İf you convert string into a array, you can easly check with in_array() method. This method will return true if given value exist in given array

<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>
Sign up to request clarification or add additional context in comments.

Comments

0

Is the p_religion column saving multiple IDs if it is a multi-select list? Would using in_array() work then instead of using $profile_data->p_religion == $religion->religion_id.

in_array ($religion->religion_id, explode(',', $profile_data->p_religion))

Added the explode() call on the off chance you are storing an imploded array.

You could also try and use the blade syntax for an if statement inline to see if it displays differently.

<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>

2 Comments

how to write in_array ($religion->religion_id, explode(',', $profile_data->p_religion)) in blade syntax.
You could wrap it in the blade curly braces. It would really just replace the == part of your if statement. {{($religion->religion_id, explode(',', $profile_data->p_religion)) ? 'selected' : ''}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.