Answer the question
In order to leave comments, you need to log in
Two associative arrays of different lengths, how to compare values and apply to checkboxes?
There are two associative arrays of different lengths, how to compare their values and apply them to checkboxes? So that if the values match, that checkbox gets the attribute checked. Now I get it like this:
@foreach($category as $categories)
<input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]"> {{$categories->title}}
@foreach($post->category as $selected)
@if($selected->title == $categories->title)
<input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]" checked> {{$categories->title}}
@endif
@endforeach
@endforeach
Answer the question
In order to leave comments, you need to log in
Hmmm, if I understood the problem correctly, then
@foreach($category as $categories)
@foreach($post->category as $selected)
@if($selected->title == $categories->title)
<input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]" checked> {{$categories->title}}
@endif
@endforeach
@endforeach
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question