我的日期是7月1日,7月2日,8月1日,sep 1,nov 1,aug 2在我的数据库中。我遍历日期,结果是这样。
如果使用雄辩相同的月份,我怎样才能获得7月1日的月份,以便结果如下?
<table>
<thead>
<tr>
<th>Test Type</th>
<?php
$types = Tblreporttype::all();
?>
@foreach($types as $type)
<?php
$dates = Tblreportdate::with('tblreporttype')
->groupBy('fDate')
->where('tblreporttype_id', '=', $type->id)
->get();
?>
@foreach($dates as $date)
<?php
$convert = $date->fDate;
$my_date = date('M/y', strtotime($convert));
?>
<th width="100">
<?php
if($my_date == $my_date) {
echo $my_date;
}
?>
</th>
@endforeach
@endforeach
</tr>
</thead>
<tbody>
<?php
$dates = Tblreportdate::with('tblreporttype')
->groupBy('fDate')
->get();
?>
<?php
$types = Tblreporttype::all();
?>
@foreach($types as $type)
<tr>
<td width="200">{{ $type->fType }}</td>
@foreach($dates as $date)
<?php
$convert = $date->fDate;
$my_date = date('d', strtotime($convert));
?>
<td width="100">
<?php
if($date->tblreporttype->id == $type->id) {
echo $my_date;
} else {
echo " ";
}
?>
</td>
@endforeach
</tr>
@endforeach
</tbody>
</table>
答案 0 :(得分:0)
我不知道如何使用Eloquent。
但你可以尝试像这样的foreach循环:
foreach($dates as $date)
{
$convert = $date->fDate;
$month = date('M', strtotime($convert));
$day = date('d', strtotime($convert));
$date_array[$month][] = $day;
}
在你的情况下应该给你一个像这样的数组:
array (size=4)
'Jul' =>
array (size=2)
0 => string '01' (length=2)
1 => string '02' (length=2)
'Aug' =>
array (size=2)
0 => string '01' (length=2)
1 => string '02' (length=2)
'Sep' =>
array (size=1)
0 => string '01' (length=2)
'Nov' =>
array (size=1)
0 => string '01' (length=2)
你可以使用foreach以你想要的方式显示它,如下所示:
@foreach ($date_array as $month => $days)
echo $month;
@foreach ($days as $day)
echo $day;
@endforeach
@endforeach
编辑:
我发现这个Eloquent / Fluent查询可以完成这项工作:
$dates = DB::table('your_table')
->select(DB::raw('MONTH(date) as month'),DB::raw('GROUP_CONCAT(DAY(date)) as days'))
->groupBy(DB::raw('MONTH(date)'))->get();
返回这样的对象:
array (size=2)
0 =>
object(stdClass)
public 'month' => int 1
public 'days' => string '1,2' (length=3)
1 =>
object(stdClass)
public 'month' => int 2
public 'days' => string '1' (length=1)