gpt4 book ai didi

php - Laravel 5.4 Blade foreach 循环

转载 作者:可可西里 更新时间:2023-11-01 00:08:14 25 4
gpt4 key购买 nike

我正在构建一个 field 管理系统,在着陆页上,我试图向访客显示所有可用的空位。已经预订的显示为不可用。我创建了两个变量,一个包含时间表中的信息,另一个包含预订表中的信息,并尝试使用 Blade 进行比较和显示。这就是我尝试在 Blade 中实现它的方式:

@foreach($times as $time)   
@foreach($bookings as $booking)
<tr>
@if($time->availble_times == $booking->booking_time)
<td>{{$time->availble_times}}: not available</td>
@else
<tr><td>{{$time->availble_times}}</td>
@endif
</tr>
@endforeach
@endforeach

但是它所做的是始终显示预订表中尽可能多的记录。就像预订表中有两行一样,它会显示两次时间,依此类推。为了以防万一,这是我的 Controller 功能:

 public function times(){
$times = Times::all();
$bookings = Bookings::all();
return view('/test2')->with('times', $times)->with('bookings', $bookings);
}

我最近开始使用 Laravel,无法弄清楚这个问题。我的问题是,如何解决 n 次显示问题并向用户显示哪些时间已预订,哪些时间可用?

最佳答案

我不知道你的数据看起来如何,但通过查看你的代码列 availble_timesbooking_time 是日期字段。如果 Times 模型是小时的字典(如 1. 8.00、2. 8:45、3. 9:00),并且预订时间必须在 Times 记录中,那么您只需要反转循环以显示每个时间预订

@foreach($bookings as $booking)
@foreach($times as $time)
<tr>
<td>{{ getImaginedChairNumber() }}</td>
<td>{{ $time->availble_times }}</td>
@if($time->availble_times == $booking->booking_time)
{{-- There is already booking for that dictionary time --}}
<td>not available</td>
@else
<td>available</td>
@endif
</tr>
@endforeach
@endforeach

应该产生类似的表格:

╔═══════╦══════╦═══════════════╗
║ Chair ║ Time ║ Booking ║
╠═══════╬══════╬═══════════════╣
║ A1 ║ 8:00 ║ not available ║
║ A1 ║ 8:45 ║ available ║
║ A1 ║ 9:00 ║ not available ║
║ A2 ║ 8:00 ║ not available ║
║ A2 ║ 8:45 ║ not available ║
║ A2 ║ 9:00 ║ not available ║
║ A3 ║ 8:00 ║ available ║
║ A3 ║ 8:45 ║ available ║
║ A3 ║ 9:00 ║ not available ║
╚═══════╩══════╩═══════════════╝

这是您期望的输出表的正确形式吗?(我用这个工具画了ascii表 https://senseful.github.io/web-tools/text-table/ )

关于php - Laravel 5.4 Blade foreach 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44487492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com