Php 使用雄辩查询进行LAVEL数据分组
我的数据库中有7月1日、7月2日、8月1日、9月1日、11月1日和8月2日的日期。我循环了一遍日期,结果是这样的 我怎样才能得到7月1日这个月,即使它是同一个月使用雄辩,这样的结果将是这样的?Php 使用雄辩查询进行LAVEL数据分组,php,mysql,date,laravel,eloquent,Php,Mysql,Date,Laravel,Eloquent,我的数据库中有7月1日、7月2日、8月1日、9月1日、11月1日和8月2日的日期。我循环了一遍日期,结果是这样的 我怎样才能得到7月1日这个月,即使它是同一个月使用雄辩,这样的结果将是这样的? 测试类型 @foreach($type作为$type) @foreach($日期作为$日期) @endforeach @endforeach @foreach($type作为$type) {{$type->fType} @foreach($日期作为$日期) @endforeach @endforea
测试类型
@foreach($type作为$type)
@foreach($日期作为$日期)
@endforeach
@endforeach
@foreach($type作为$type)
{{$type->fType}
@foreach($日期作为$日期)
@endforeach
@endforeach
我不知道如何使用雄辩的语言
但您可以尝试这样的foreach循环:
foreach($dates as $date)
{
$convert = $date->fDate;
$month = date('M', strtotime($convert));
$day = date('d', strtotime($convert));
$date_array[$month][] = $day;
}
@foreach ($date_array as $month => $days)
echo $month;
@foreach ($days as $day)
echo $day;
@endforeach
@endforeach
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)
在您的情况下,这将为您提供如下数组:
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($dates as $date)
{
$convert = $date->fDate;
$month = date('M', strtotime($convert));
$day = date('d', strtotime($convert));
$date_array[$month][] = $day;
}
@foreach ($date_array as $month => $days)
echo $month;
@foreach ($days as $day)
echo $day;
@endforeach
@endforeach
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)
编辑:
我发现这个雄辩/流畅的查询应该可以完成这项工作:
$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();
返回如下对象:
foreach($dates as $date)
{
$convert = $date->fDate;
$month = date('M', strtotime($convert));
$day = date('d', strtotime($convert));
$date_array[$month][] = $day;
}
@foreach ($date_array as $month => $days)
echo $month;
@foreach ($days as $day)
echo $day;
@endforeach
@endforeach
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)
你至少可以发布你当前的代码吗?输出是不同的,但想法仍然相同。我如何才能添加空格,使其与月份一致?