本文介绍了Laravel 5.4刀片foreach循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个场地管理系统,并且在着陆页上,我试图向访问者显示所有可用的广告位.已预订的客房显示为不可用.我创建了两个变量,一个变量携带时间表中的信息,另一个变量携带预订表中的信息,并尝试使用Blade进行比较和显示.这就是我试图在Blade中实现它的方式:

I am building a venue manangement system, and on the landing page, i m trying to show all the available slots to the visitors. The ones that have already been booked are shown as not available. I have created two variables, one that carries the info from the time table and the other from the booking table and tyring to use blade to compare and show.This is how I am trying to implement it in 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

但是,这实际上是始终显示预订表中尽可能多的记录.就像预订表中有两行一样,它显示两次时间,依此类推.以防万一,这是我的控制器功能:

But what this does instead is show all the times for as many records in the bookings table. Like if there are two rows in the bookings table, it shows the times twice, and so on. Just in case, here is my controller function:

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

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

I recently started doing Laravel and cannot figure out this issue. My question is, how can i fix the n-times display issue and show the user which times are booked and which are available?

推荐答案

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

I don't know how your data looks, but by looking at your code columns availble_times and booking_time are date fields. If Times model is dictionary for hours (like 1. 8.00, 2. 8:45, 3. 9:00) , and booking hour has to be in Times records, then you just need to invert the loops to display each time for each booking

@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/)

Is this the correct form of the output table you expect?(I used this tool to draw ascii table https://senseful.github.io/web-tools/text-table/)

这篇关于Laravel 5.4刀片foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 10:01