2014-10-04 54 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