Laravel 5.1 Blade(或只是简单的回显)错误地显示 类

Laravel 5.1 Blade (or just plain echo) displaying classes incorrectly

我的 Laravel (5.1) 视图中有以下代码。 PHP 块中的代码决定我应该将哪个 class 应用于下面的第一个 td 元素。

@foreach($datesUnavailable as $dateUnavailable)
    <?php
        $unavailableDate = strtotime($dateUnavailable->unavailable_on);
        $creationDate = strtotime($dateUnavailable->created_at);
        $newformat = date('D, M d, Y',$unavailableDate);

        $createdBefore = ($unavailableDate - $creationDate)/86400;

        if($createdBefore >= 5) {
            $class = "alert alert-success";                                           
        } else if ($createdBefore < 5 && $createdBefore >= 3) {
            $class = "alert alert-warning"; 
        } else {
            $class = "alert alert-danger";
        }
    ?>
    <tr>
        <td class={{ $class }}>{{ $newformat }}</td>
        <td>{{ $dateUnavailable->unavailability_type }}</td>
        <td>
            {!! Form::open([
                'method' => 'DELETE',
                'route' => ['delete-unavailability', $alias, $dateUnavailable->uc_key],
                'onsubmit' => 'return submitResult();'
            ]) !!}
                {!! Form::submit('Delete', ['class' => 'btn btn-danger btn-xs']) !!}
            {!! Form::close() !!}
        </td>
    </tr>
@endforeach

但是,当页面呈现时,td 元素上的 classes 显示为

<td class="alert" alert-success="">Wed, Feb 28, 2018</td>

也就是说,我希望它显示 class="alert alert-success"(基于逻辑)但它显示了这个奇怪的字符串。如果我执行 var_dump($class),我会看到 $class 变量的值为 "alert alert-success"。

知道为什么会这样吗?

这应该适合你:

<td class="{{ $class }}">

添加缺少的 " 个标记。

@foreach($datesUnavailable as $dateUnavailable)
    <?php
        $unavailableDate = strtotime($dateUnavailable->unavailable_on);
        $creationDate = strtotime($dateUnavailable->created_at);
        $newformat = date('D, M d, Y',$unavailableDate);

        $createdBefore = ($unavailableDate - $creationDate)/86400;

        if($createdBefore >= 5) {
            $class = "alert alert-success";                                           
        } else if ($createdBefore < 5 && $createdBefore >= 3) {
            $class = "alert alert-warning"; 
        } else {
            $class = "alert alert-danger";
        }
    ?>
    <tr>
        <td class="{{ $class }}">{{ $newformat }}</td> <!-- Changed this line -->
        <td>{{ $dateUnavailable->unavailability_type }}</td>
        <td>
            {!! Form::open([
                'method' => 'DELETE',
                'route' => ['delete-unavailability', $alias, $dateUnavailable->uc_key],
                'onsubmit' => 'return submitResult();'
            ]) !!}
                {!! Form::submit('Delete', ['class' => 'btn btn-danger btn-xs']) !!}
            {!! Form::close() !!}
        </td>
    </tr>
@endforeach