A
A
Analka2020-07-06 14:55:53
Laravel
Analka, 2020-07-06 14:55:53

Mark existing items when listing?

When editing a record, a list of groups is displayed

@forelse($groups as $group)
      <div class="sBsGroupsItm">
          <i class="fas fa-tags"></i>
          <label for="group_{{$group->id}}">
                 <input type="checkbox" id="group_{{$group->id}}" name="groups[]" value="{{$group->id}}">
                  {{$group->name}}
         </label>
    </div>
@empty
@endforelse


how to mark (checked) those id's that exist in the array when outputting forelse
$nGroups = array:2 [▼
  0 => "1"
  1 => "3"
]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem, 2020-07-06
@Analka

<input type="checkbox" id="group_{{$group->id}}" name="groups[]" value="{{$group->id}}"{{ in_array( $group->id, $nGroups ) ? ' checked' : '' }}>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question