编程语言
首页 > 编程语言> > php-Laravel 5.1 Blade(或只是简单的回显)错误地显示了类

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

作者:互联网

我的Laravel(5.1)视图中有以下代码. PHP块中的代码决定了我应将哪个类应用于下面的第一个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元素上的类显示为

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

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

知道为什么会这样吗?

解决方法:

添加缺少的“”标记.

@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

标签:laravel-blade,laravel,laravel-5-1,php
来源: https://codeday.me/bug/20191025/1927962.html