D
D
Denis Rybin2015-02-18 09:26:33
Laravel
Denis Rybin, 2015-02-18 09:26:33

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

4 answer(s)
S
Sergey Melnikov, 2015-02-18
@mlnkv

What are you doing like that?

A
Alexey Yakhnenko, 2015-02-18
@ayahnenko

For example, you are not very good at asking questions :)

E
Evgeny Kalibrov, 2015-02-18
@rework

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

plus your condition executes exactly the same code, whether it is met or not. If the condition is met, you need to add the "checked" attribute to the input, so that you can be told how to do it, can you at least tell me what kind of template engine it is, or what kind of CMS?

V
Vyacheslav Barsukov, 2015-02-22
@slavabars

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 question

Ask a Question

731 491 924 answers to any question