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>
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.
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.
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.
@php
$males = $children->where('gender', 'Man');
@endphp
@foreach($children as $male) // should be foreach($males as $male)
{{ $child->name }} // should be $male->name
@endforeach