0

問題があります。Blade は @else ステートメント内のコードを実行していません。これが私のコードです(これもテーブルです):

        @extends('base')
@section('title', 'Who is online?')
@endsection
@section('main')

<div class="doc-content-box">
  <table class="table table-striped table-condensed">
    <thead>
      <tr>
        <th>#</th>
        <th>Name</th>
        <th>Level</th>
        <th>Vocation</th>
        <th>Date registered</th>
        <th>Role</th>
        </tr>
  </thead>
<tbody>

    @if(!empty($results))
        <?php $count = 0;?>
        @foreach($results as $result) 
      <?php $count++;?>
      <tr>

        <td>{{ $count }}. </td>

        <td>{{ $result->name }}</td>

        <td>{{ $result->level }}</td>

        <td><?php if($result->vocation == 1){ 
echo "Sorcerer"; 
}else if($result->vocation == 2){
echo 'Druid';
}else if($result->vocation == 3){
echo 'Paladin';
}else if($result->vocation == 4){
echo 'Knight';
}else if($result->vocation == 5){
echo 'Master Sorcerer';
}else if($result->vocation == 6){
echo 'Elder Druid';
}else if($result->vocation == 7){
echo 'Royal Paladin';
}else{
echo 'Elite Knight';
}?></td>

        <td>{{ $result->created_at }}</td>

        <td><?php if($result->group_id == 1){ 
echo "Player"; 
}else if($result->group_id == 2){
echo 'Gamemaster';
}else if($result->group_id == 3){
echo 'God';
}?></td>

      </tr>
      @endforeach 
@else

<td>{{ "There are no users online." }}</td>

@endif
</tbody>

  </table>
</div>
</div>

@endsection

ただ実行されません オンラインのプレイヤーはいません。また、 @else 節を実行していない Blade テンプレートを<tr></tr>表示し、次のようなことを試しました: 、それでも表示されません。<td>'There are no players online.'</td>

また、これらすべての php if ステートメントを三項演算子で切り替えることはできますか? もしそうなら、どうすればできますか?

4

2 に答える 2

4

その理由は、ifステートメントがステートメントの内部にあるため、foreach実行されないためです。ブロックのforeach内側を移動すると、機能するはずです。if

- - もっと

私は実際にあなたがしていたことをしなければならず、問題を発見しました。これはオブジェクトempty()$resultsあり、配列ではなく、Collectionオブジェクトです。正しい方法は、count()メソッドを使用することです。だから、このようなもの:

@if($results->count() > 0)
    @foreach($results as $result)
        ...
    @endforeach
@else
    <p>Nothing found!</p>
@endif
于 2013-07-31T14:58:02.183 に答える
2

これは、laravel のブレード テンプレートで三項演算子を記述する方法の 1 つです。

{{{$collectionObject->filed_name ==1 ? 'Yes' : 'No' }}}
于 2014-03-14T23:57:46.523 に答える