I am trying to use dynamic tabs in laravel and iam kinda new to it. Iam stuck in it for like 2 days.
I have two variables that getting data from tables in mysql.
$stations = Station::select('id', 'name')->get();
// return $staions;
$queues = Queue::withTrashed()
->oldest()
->get();
// return $queues;
The data in $schedules
:
[
{
"id":1,
"name":"siinay"
},
{
"id":2,
"name":"jigjiga"
},
{
"id":3,
"name":"Idaacada"
},
{
"id":4,
"name":"Xero Awr"
}
]
And the data in the $queues
:
So the problem is here in dynmaic tabs: I have wanted to display station names in the nav-tabs and queues in the tab-content . So i did it like this.
<ul class="nav nav-tabs">
@forelse ($stations as $station)
<li >
<a data-toggle="tab" href="#tab-{{ $station->id }}" >
{!! $station->name !!}
</a>
</li>
@empty
@endforelse
</ul>
<div style="margin-top: 15%" class="tab-content">
@foreach($queues as $queue)
@if($queue)
<div id="tab-{{ $queue->station_id}}" class="tab-pane fade">
<p>{!! $queue->bus_number !!}</p>
</div>
@else
<div>empty</div>
@endif
@endforeach
</div>
when i run the above code, it only showing the last queue bus_number
per each sttions. every stations queue
Oldest is indeed the oldest, it's the opposite to first. Try this to get a collection:
$queues = Queue::withTrashed()
->orderBy('id', 'desc')
->get();
In the view (simplified):
<ul class="nav nav-tabs">
@foreach ($stations as $station)
<li>
<a data-toggle="tab" href="#tab-{{ $station->id }}">
{{ $station->name }}
</a>
</li>
@endforeach
</ul>
<div style="margin-top: 15%" class="tab-content">
@foreach($queues as $queue)
<div id="tab-{{ $queue->station_id }}" class="tab-pane fade">
<p>{{ $queue->bus_number }}</p>
</div>
@endforeach
</div>
No need to check for empty. Each one is a table record. Unless you want to check on bus_number or station_id.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.