Answer the question
In order to leave comments, you need to log in
How to properly display checked checkboxes?
Hello, help
I need to make sure that the checkboxes to which the article belongs are marked (I pass the object of the article and all tags to the template), I do this in the template:
@foreach($tegs as $teg)//all tags
@foreach($article->tegs as $articleTeg)//tags related to the article
@if($teg->id == $articleTeg->id)//if id matches
{{$teg->name}}
@else//does not match
{{$ teg->name}}
@endif
@endforeach
@endforeach
I'm displaying all tags twice, where is the error? Maybe you need to somehow show the checked checkboxes differently?
Answer the question
In order to leave comments, you need to log in
Twice, apparently because you have a cycle in a cycle inserted. In my opinion, this is wrong here, because. variable array is the same pereberatsya, hike.
Purely intuitively I will suggest:
foreach($article->tegs as $articleTeg)
@if($teg->id == $articleTeg->id)
{{$teg->name}}
@else
{{$teg->name}}
@endif
@endforeach
Rewrite the database query using join. Such code is bad form.
If this is not data from the database, then work with the object as with an array and look for one in the other through in_array ()
In general, in your code, under any condition, the tag name is displayed. Remove else
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question