Support the ongoing development of Laravel.io →
Database Views Blade

I have two arrays of objects and I want to check if ids of elements of both are the same then select option in select box otherwise just display it. I tried like this:


<select multiple class="form-control" name="category_id[]">
                                @if(!$product->categories->isEmpty())
                                    @foreach($mysliwski as $mysl)
                                        @foreach($product->categories as $cat)
                                            @if($mysl->id == $cat->id)
                                                <option selected value="{{$mysl->id}}">{{$mysl->name}}</option>
                                            @else
                                                <option value="{{$mysl->id}}">{{$mysl->name}}</option>
                                            @endif
                                        @endforeach
                                    @endforeach
                                @else
                                    @foreach($mysliwski as $mysl)
                                        <option value="{{$mysl->id}}">{{$mysl->name}}</option>
                                    @endforeach
                                @endif
                                </select>

But it works only when one category is the same. When there are more my select option are duplicated. Here are my two arrays http://laravel.io/bin/ro5rM Where I have bug?

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Zolax zolax Joined 21 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.