Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ibe's avatar
Level 1

How to show children based on gender

Hi there, does anyone know how I can show only children based on their gender ('man' or 'vrouw')

I my application I made some 3 tabs: first tab shows all the children, second tab should show all the boys and the third tab should show al the girls. I am able to show all the children, but how can I 'filter' the foreach loop so it only selects the children that are boys or girls.

Everytime I add a new child you can add the gender. This passes a string value 'man' or 'vrouw' to the database.

Blade:

<b-tabs position="is-centered" class="block">
            <b-tab-item label="All"> <!--first tab-->
              @foreach ($children as $child)
              <div class="field" class="checkbox" >
                <b-checkbox name="children[]" native-value="{{$child->id}}" class="checkbox">
                  <div class="checkbox">
                    <figure class="image is-64x64">
                      <img src="/images/children/{{$child->photo}}" class="rounded-image" alt="Placeholder image">
                    </figure>
                    {{$child->name}}
                  </div>

                </b-checkbox>
              </div>
              @endforeach
            </b-tab-item> <!--end first tab-->


            <b-tab-item label="Boys"> <!--second tab-->

              @foreach ($children as $child)

                <!--loop around children with gender = 'man'-->

              @endforeach

            </b-tab-item> <!--end second tab-->

            <b-tab-item label="Girls"> <!--third tab-->
              @foreach ($children as $child)

                <!--loop around children with gender = 'man'-->

              @endforeach
            </b-tab-item><!--end third tab-->
          </b-tabs>

Thanks!

0 likes
17 replies
Cronix's avatar

Can you show an example of what dd($children) returns?

There is a where() collection method where you can get items from a collection that match.

Something like

<h2>Males</h2>
@php
    $males = $children->where('gender', 'man');
@endphp
@foreach($males as $male)
    {{ $male->name }}
@endforeach

<h2>Females</h2>
@php
    $females = $children->where('gender', 'vrouw');
@endphp
@foreach($females as $female)
    {{ $female->name }}
@endforeach 

But it depends on what your data looks like.

https://laravel.com/docs/5.6/collections#method-where

1 like
Cronix's avatar

You can make it a bit more robust and show a message if there aren't any of one type or the other

<h2>Females</h2>
@php
    $females = $children->where('gender', 'vrouw');
@endphp

@if ($females->count())
    @foreach($females as $female)
        {{ $female->name }}
    @endforeach 
@else
     <div>Sorry, there are no female children.</div>
@endif

Personally, I'd do the $females = $children->where('gender', 'vrouw'); and same for males in the controller (after you've run the children query), and pass them to the view like you are with $children, just to keep the view cleaner.

1 like
devk's avatar

Another option:

@foreach ($children->filter(function ($i) {return $i->gender === 'man';} as $child)

    <!--loop around children with gender = 'man'-->

@endforeach
1 like
Ibe's avatar
Level 1

@Cronix dd gets me: http://prntscr.com/j9l4jp but its not working.. I tried this:

        @php
              $males = $children->where('gender', 'man');
              @endphp

              @foreach($children as $male)

              {{ $child->name }}

              @endforeach 

But its not working either..

devk's avatar

Actually, as Cronix suggested, ->where just seems the nicest. You could do:

@foreach ($children->where('gender', 'male') as $male)

    <!--loop around children with gender = 'man'-->

@endforeach
1 like
Cronix's avatar

Do the dd() again and click on one of the Child elements (the arrow in the graphic) to expand it so I can see the child's properties. I was just guessing the name based on what you stated. It might not be called "gender", or it might be in another object.

1 like
devk's avatar

Whether the collection is empty, or the result is empty or not, this error doesn't happen because of the foreach. I think you have the error somewhere else.

1 like
devk's avatar

@Cronix If it's a model, it wouldn't. The property would simply return null and it would return false (so it wouldnt be in the returned result).

If it's a different object, the error should be something like Undefined property ..

1 like
Cronix's avatar

Ok, so it's "Man" and not "man". Check what it is for female too and adjust.

$males = $children->where('gender', 'Man');
1 like
Cronix's avatar
Cronix
Best Answer
Level 67

Also, your code is not the same as mine...

@php
$males = $children->where('gender', 'Man');
@endphp

@foreach($children as $male)  // should be foreach($males as $male)

    {{ $child->name }} // should be $male->name

@endforeach 
1 like
Ibe's avatar
Level 1

@cronix @devk yh it was Man with capital letters, sorry stupid of me :p But really thanks guys!!

Cronix's avatar

No problem. If it's working now, please mark the issue as solved.

devk's avatar

Out of pure curiosity I tried this in a sample project:

$videos = App\Video::get();
return $videos->where('nonexistant property', 'male');

The result was:

[]

$videos initially had 6 objects in it.

It didn't throw an error. The offset error has to have been somewhere else

Please or to participate in this conversation.