I have this code foreach loop it will get the first data form my table every iteration on the next output. I would like to have a guidance on this

<select class="selectpicker form-control" data-live-search="true">

@foreach ($IncidentTypes as $Incident)

<optgroup label="{{ $Incident->incident_type ." ". $Incident->id }}">

@foreach ($Incident->IncidentSubType as $subTypes)
<option>
{{ $subTypes->incident_name . " " . $subTypes->incident_type_id }}
</option>

</optgroup>
@endforeach
@endforeach

</select>

Screenshot_2022-09-29_002928.png

Screenshot_2022-09-29_002922.png

Recommended Answers

All 5 Replies

Hello and welcome to DaniWeb! I really wish I could help you, but, while I have a lot of PHP experience, I don't have any experience with Laravel or its templating system.

Does the code you have not work? Is it giving you any errors? I can try to help you debug it if you can explain in what way you're looking for guidance.

Screenshot_2022-09-29_090522.png

Screenshot_2022-09-29_090536.png

on my database tables Incident_Category and Incident_Sub_Category I need to populate the code above regarding the dropdown option on which every set it repeats the first data which is Animal Bite - 1 repeating the label on top would be the main ID on which the child data will be the same number
Medical - ID-1
Animal Bite - ID-1

Police - ID-2
Animal Bite - ID-1 (this should not repeats)
Rape - ID-2

Fire - ID-3
Animal Bite - ID-1 (this should not repeats)
School Fire- ID-3

By the time we get to this template, are the variables $IncidentTypes and $subTypes that are being looped through arrays that contain the correct data? What are the contents of each of them at the time we get to this HTML snippet?

to answer your question the varialbes on the controller

$IncidentTypes = IncidentType::all();
$IncidentSubTypes = IncidentSubType::all();

I understand that. My question was can you please tell me what those arrays look like. e.g.

array(
    'category' => array(
        'item1',
        'item2'
) etc ...

It seems like the problem you're having is that the data in these arrays is not in the format that your view templates are expecting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.