Foreach Laravel 4

时间:2014-10-04 15:16:49

标签: php laravel laravel-4 foreach

只是我或它的foreach循环表现得很奇怪。我已经做了一个foreach来检查是否有结果,但是如果没有,我会显示一个通知/文本,确认现在有结果。这里的问题是,如果它没有返回任何内容,我就不会显示任何文本。这里似乎有什么问题?

@extends('layouts.master')
@section('content')
     {{ Form::open(['url'=>'flight/onewayflightresults']) }}          
        <div>
            <p>Scheduled Flights</p>
        </div>
        <table class="table table-hover">
                    <th>Reserve</th>
                    <th>#</th>
                    <th>From</th>
                    <th>To</th>
                    <th>Departure</th>
                    <th>Adult</th>
                    <th>Children</th>
                    <th>Fare per person</th>
                    <th>Book Flight</th>
            @foreach($result as $row)
                @if(count($row) != 0)

                        <tr>
                            <td>{{Form::checkbox('reserve', 'value');}}</td>
                            <td>{{$row -> id}}</td>
                            <td>{{$row -> destinationfrom}}</td>
                            <td>{{$row -> destinationto}}</td>
                            <td>{{$row -> departure}}</td>
                            <td>{{{$adult}}}</td>
                            <td>{{{$child}}}</td>
                            <td>&#x20b1;{{{$row -> fare}}}</td>
                            <td>{{ Form::submit('&nbsp;&nbsp;&nbsp;&#10004;&nbsp;&nbsp;&nbsp;',array('class'=>'btn btn-success')) }}</td>
                        </tr>       
                @else 
                    <p class="bg-danger">Sorry but we have no available flight schedule on your desidered date.</p>
                @endif 
            @endforeach
       </table>
     {{ Form::close() }}
@endsection  

2 个答案:

答案 0 :(得分:1)

你试图计算$row,而它是一个对象。我不确定,但也许你想要做这种逻辑呢?

@if (count($result))
    @foreach($result as $row)
        <!-- output rows -->
    @endforeach
@else
    <!-- output no flights found -->
@endif

答案 1 :(得分:1)

您还可以使用forealse构造:

@forelse($result as $row)
 <tr>
    {{-- here single row code -- }}
 </tr> 
@empty
     <p class="bg-danger">Sorry but we have no available flight schedule on your desidered date.</p>
@endforelse