我目前正在使用foreach循环在视图中显示项目。
@foreach ($projects as $project)
<div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
{!! Form::open(array('class' => 'form-inline delete', 'method' => 'DELETE', 'route' => array('projects.destroy', $project->id))) !!}
<div class="panel panel-default">
<div class="panel-heading projectPanelHeading">
<h4>{{ $project->projectName }}</h4>
</div>
<div class="panel-body projectPanel">
<p>Deployment Date: {{ ($project->deploymentDate ? date('d-m-Y', strtotime($project->deploymentDate)) : '') }}</p>
<p>Status: {{ $project->status or '' }}</p>
</div>
</div>
{!! Form::close() !!}
</div>
@endforeach
如您所见,每个项目都有一个$ project-&gt; deploymentDate,我将其转换为格式为d-m-Y的日期对象。
我要做的是将deploymentDate与当前日期进行比较,如果已经过了deploymentDate,则将面板红色类应用于面板。
我正在考虑做这样的事情
{!! Form::open(array('class' => 'form-inline delete', 'method' => 'DELETE', 'route' => array('projects.destroy', $project->id))) !!}
@if(date('d-m-Y', strtotime($project>deploymentDate)) < date('d-m-Y'))
<div class="panel panel-default panel-red">
<div class="panel-heading projectPanelHeading">
<h4>{{ $project->projectName }}</h4>
</div>
<div class="panel-body projectPanel">
<p>Deployment Date: {{ ($project->deploymentDate ? date('d-m-Y', strtotime($project->deploymentDate)) : '') }}</p>
<p>Status: {{ $project->status or '' }}</p>
</div>
</div>
@else
<div class="panel panel-default">
<div class="panel-heading projectPanelHeading">
<h4>{{ $project->projectName }}</h4>
</div>
<div class="panel-body projectPanel">
<p>Deployment Date: {{ ($project->deploymentDate ? date('d-m-Y', strtotime($project->deploymentDate)) : '') }}</p>
<p>Status: {{ $project->status or '' }}</p>
</div>
</div>
@endif
{!! Form::close() !!}
我不喜欢这种方法是重复面板块。此外,面板红色应用于deploymentDate尚未通过的项目时会出现很多错误,反之亦然。
有没有更好的方法来实现我的目标是正确的,并且不需要重复?
由于
答案 0 :(得分:1)
首先,将deploymentDate
属性添加到Project模型中的$dates
数组中。这将确保您拥有deploymentDate
的Carbon实例。
protected $dates = ['deploymentDate'];
一旦实现,您可以通过简单的功能检查它是否小于:
@if($project->deploymentDate->lt(\Carbon\Carbon::Now()))
这也可以通过Project.php
中的模型上的方法来实现public function checkDeploymentDate()
{
return $this->deploymentDate->lt(\Carbon\Carbon::Now());
}
然后在你的观点中:
@if($project->checkDeploymentDate())
最后,对于面板部分,您可以为其添加Blade指令:
<?php
namespace App\Providers;
use Blade;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
Blade::directive('deploymentDate', function($expression) {
return "<?php echo($expression); ?>";
});
}
}
Blade指令将帮助您将新功能扩展到Blade模板。所以在上面的例子中,在你的视图模板中:
{{@deploymentDate(test)}}
将回显测试