【发布时间】:2015-07-22 19:17:41
【问题描述】:
我安装了 Laravel 5.1,并且我有一个包含各种列的提示数据库...一列是提示相关的日期。
因此我只想显示日期等于今天日期的表格行。
我已经设法得到它,所以我可以运行一个 foreach 来循环浏览每个提示,但需要一个特定的来显示与今天日期匹配的提示
任何都会很棒:)
这是我的代码
提示控制器
public function index() {
$tips = Tips::whereDate('tip_date','=', strftime("%y-%m-%d", strtotime(\Carbon\Carbon::now())))->get();
return view ('tips.index', compact ('tips'));
}
提示(模型)
protected $fillable = [
'tip_date',
'home_team',
'away_team',
'league',
'tip',
'odds',
'score',
'result'
];
protected $dates = ['tip_date'];
Index.Blade.PHP(视图)
<div class="align-center">
<h1><?php echo date("dS F Y");?></h1>
@foreach($tips as $tip)
@if (date("Y-m-d",strtotime($tip['tip_date'])) == date("Y-m-d"))
<h3>{{ $tip->league }}</h3>
<h2>{{ $tip->home_team }} V {{ $tip->away_team }}</h2>
<h3><b>{{ $tip->tip }}</b> @ {{ $tip->odds }}</h3>
@endif
@endforeach
</div>
</div>
<table class="table table-striped">
<thead>
<tr>
<th>Date</th>
<th>Match</th>
<th>Tip</th>
<th>Odds</th>
<th>Score</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<?php $tipcount=0; ?>
@foreach($tips as $tip)
@if (date("Y-m-d",strtotime($tip['tip_date'])) < date("Y-m-d"))
<tr>
<th scope="row">{{ $tip->tip_date }}</th>
<td>{{ $tip->home_team }} V {{ $tip->away_team }}</td>
<td>{{ $tip->tip }}</td>
<td>{{ $tip->odds }}</td>
<td>{{ $tip->score }}</td>
<td>{{ $tip->result }}</td>
</tr> @endif
<?php
$tipcount++;
if($tipcount===30){break;};
?>
@endforeach
【问题讨论】:
-
是否可以选择使用数据库仅返回今天的行?还是您需要返回所有记录然后解析它们以获取今天的提示?
-
如果你得到今天的所有提示而不是得到所有提示然后通过循环过滤会更好。
-
我需要让今天的提示显示在页面顶部,然后在表格中显示最后 30 个提示,不包括下面的今天,都在同一页面/视图上
标签: model-view-controller controller laravel-5 laravel-5.1