php Laravel 5.4 刀片 foreach 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44487492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:59:37  来源:igfitidea点击:

Laravel 5.4 blade foreach loop

phplaravel-5laravel-5.4laravel-blade

提问by Muhammad Muneeb ul haq

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);
}

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?

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

回答by Bart?omiej Sobieszek

I don't know how your data looks, but by looking at your code columns availble_timesand booking_timeare 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

我不知道你的数据看起来如何,但通过查看你的代码列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

Should produce similar table:

应该产生类似的表:

╔═══════╦══════╦═══════════════╗
║ 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 ║
╚═══════╩══════╩═══════════════╝

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

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