我在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 =&#34; alert alert-success&#34; (基于逻辑)但它显示这个奇怪的字符串。如果我执行var_dump($ class),我会看到$ class变量的值为&#34; alert alert-success&#34;。
知道为什么会这样吗?
答案 0 :(得分:2)
这应该适合你:
<td class="{{ $class }}">
答案 1 :(得分:2)
添加缺少的"
标记。
@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