如何在@foreach循环

时间:2017-07-13 12:41:32

标签: php laravel

我正在尝试对此做一些澄清,因为我尝试了不同的方法,但它似乎对我没有用。

我有一个像这样的foreach循环:

@foreach($mods as $key => $value)
                <tr class="table-tr draggable" data-id="{!!$value->id!!}" style="background-color: {{$value->mod_colour}}; color: {{$value->mod_text_colour}};">
                    <!-- This creates the Mod -->
                    <td class="startTime"></td>
                    <td class="endTime"></td>
                    <td class="duration">{{ $value->mod_duration }}</td>
                    <td class="intent">{{ $value->mod_intent }}</td>
                    <td class="module">
                        @if ($value->mod_module != null)
                            {{ $value->mod_module }}
                        @else
                            <table class="table-bordered" style="width:100%; text-align: center; color: white;">
                                @for ($i = 1; $i <= $value->mod_team; $i++)
                                    <td style="padding:10px;">Team {{ $i }}</td>
                                @endfor
                            </table>
                        @endif
                    </td>
                    <td class="output">{{ $value->mod_output }}</td>
                    <td class="input">{{ $value->mod_input }}</td>
                    <td class="logistics">{{ $value->mod_logistics }}</td>
                    <td>
                        <a class="btn btn-small btn-info" style="margin:auto; margin-top: 10px; margin-bottom: 10px; margin-left:10px; margin-right:10px; display:block;" href="" data-target="#modal-edit-mod{{ $value->id }}" data-toggle="modal" id="modal-edit">Edit</a>
                    </td>
                </tr>
@endforeach

我面临的问题是,当数据库中没有mod,因此没有任何内容传递给$ value变量时,页面将抛出一个'未定义变量:value '错误。我想要实现的是创建一些catch语句,如果没有定义值,那么它将默认为'Create New Mod'屏幕或模态。我尝试了以下内容:

@foreach($mods as $key => $value)
                @if($value != null)
                <tr class="table-tr draggable" data-id="{!!$value->id!!}" style="background-color: {{$value->mod_colour}}; color: {{$value->mod_text_colour}};">
                    <!-- This creates the Mod -->
                    <td class="startTime"></td>
                    <td class="endTime"></td>
                    <td class="duration">{{ $value->mod_duration }}</td>
                    <td class="intent">{{ $value->mod_intent }}</td>
                    <td class="module">
                        @if ($value->mod_module != null)
                            {{ $value->mod_module }}
                        @else
                            <table class="table-bordered" style="width:100%; text-align: center; color: white;">
                                @for ($i = 1; $i <= $value->mod_team; $i++)
                                    <td style="padding:10px;">Team {{ $i }}</td>
                                @endfor
                            </table>
                        @endif
                    </td>
                    <td class="output">{{ $value->mod_output }}</td>
                    <td class="input">{{ $value->mod_input }}</td>
                    <td class="logistics">{{ $value->mod_logistics }}</td>
                    <td>
                        <a class="btn btn-small btn-info" style="margin:auto; margin-top: 10px; margin-bottom: 10px; margin-left:10px; margin-right:10px; display:block;" href="" data-target="#modal-edit-mod{{ $value->id }}" data-toggle="modal" id="modal-edit">Edit</a>
                    </td>
                </tr>
                @else
                <p>No mods!</p>
                @endif
            @endforeach

它会检查$ value变量是否为null,但是没有效果。我也尝试了 - &gt; isEmpty()方法,但它仍然返回未定义的变量错误。

我想要帮助的是指向创建@if / @ else语句的最佳方法的指针,它可以检查$ value变量是否具有值,或者是否有更好的方法来实现我正在努力做什么。

谢谢!

6 个答案:

答案 0 :(得分:3)

您可以使用isset()empty()

@foreach($mods as $key => $value)
    @if(isset($value)) 

@foreach($mods as $key => $value)
    @if(!empty($value)) 

答案 1 :(得分:1)

您可以使用isset()方法!

类似的东西:

if (isset($var)) {}

如果设置了$ var,则该方法返回true,否则返回false。

祝你好运!

答案 2 :(得分:0)

检查变量是否定义为isset(),例如:

if(isset($value)){
   echo $value;
}

答案 3 :(得分:0)

您应该使用isset($ value)来检查变量是否已设置。

同样在这种情况下,您可以使用两者来检查变量是否设置而不是空

if(isset($value)) && is empty($value))
{
    \\ Your Code
}

答案 4 :(得分:0)

为了更清楚地说明,变量的定义与否的问题是不同的,变量是否为空,并且检查它的方式也不同。

我给你一些例子。

// create variable empty and fill it with null/''/0

$empty = null; // I set it with null

// And we create another variable notEmpty
$notEmpty = 'Not Empty';

if(empty($empty)) {
    echo "Empty"; // It will echo Empty if we run it
}

if(empty($notEmpty)) {
    echo "Empty"; // Not called
} else {
    echo "Not Empty"; // Called
}

// Now checking if variable is defined or not
if(isset($otherVar)) {
   echo "Defined";
} else {
   echo "is Not Defined";
}

正如您可以看到的那样,您可以选择以下两种方法:-D

答案 5 :(得分:0)

你可以在laravel中做到这一点。我检查你的$ mods的结果,看看是否有任何东西,然后执行,如果需要:

@if(is_null($mods))
  //its null, do this code
@else
your current code if a var is found
@endif