2014-10-04 53 views
0

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

@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 

回答

1

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

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

感谢您的时间和答复。这工作像魔术一样。 – staphhelpme 2014-10-04 16:00:47

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