Laravel 4 else 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17973754/
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-09-14 08:12:51  来源:igfitidea点击:

Laravel 4 else statement

laravellaravel-4

提问by dynamitem

I've got a problem. Blade is not executing the code within the @else statement. Here's my code (also, it's a table):

我有问题。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

It just won't execute There are no players online. I've also tried adding it in between <tr></tr>, also viewed Blade template not executing @else clause, tried something like: <td>'There are no players online.'</td>, but still it doesn't show up.

它只是不会执行 没有在线玩家。我也试过在两者之间添加它<tr></tr>,还查看了Blade 模板没有执行 @else 子句,尝试了类似的东西:<td>'There are no players online.'</td>,但它仍然没有出现。

Also, am I able to switch all those php if statements with ternary operators? If so, how could I do it?

另外,我是否可以使用三元运算符切换所有这些 php if 语句?如果是这样,我该怎么做?

回答by jasonlfunk

The reason is because your ifstatement is inside of your foreachstatement and is therefore never executed. If you move your foreachinside of your ifblock, it should work.

原因是因为你的if语句在你的foreach语句中,因此永远不会被执行。如果您移动块的foreach内部if,它应该可以工作。

--- More

- - 更多的

I actually just had to do what you were doing and discovered the problem. You are calling empty()on $resultswhich is an object and is not an array, it is a Collectionobject. The proper way is to use the count()method. So something like this:

我实际上只需要做你正在做的事情并发现问题。您呼叫empty()$results是一个对象,是不是一个数组,它是一个Collection对象。正确的方法是使用count()方法。所以像这样:

@if($results->count() > 0)
    @foreach($results as $result)
        ...
    @endforeach
@else
    <p>Nothing found!</p>
@endif

回答by kdhayal

This is one of the way to write a ternary operators in blade template in laravel.

这是在laravel的blade模板中编写三元运算符的一种方式。

{{{$collectionObject->filed_name ==1 ? 'Yes' : 'No' }}}