【问题标题】:Laravel Date Grouping using Eloquent Query使用 Eloquent 查询的 Laravel 日期分组
【发布时间】:2014-07-16 06:36:05
【问题描述】:

我的数据库中有 7 月 1 日、7 月 2 日、8 月 1 日、9 月 1 日、11 月 1 日、8 月 2 日的日期。我遍历日期,结果是这样。

如何使用 eloquent 获得 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>

【问题讨论】:

  • 您至少可以发布您当前的代码吗?
  • 输出不同但思路还是一样

标签: php mysql date laravel eloquent


【解决方案1】:

我不知道如何使用 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)

【讨论】:

  • 如何添加空格使其与月份对齐?
猜你喜欢
  • 1970-01-01
  • 2020-10-30
  • 2015-10-20
  • 2016-06-02
  • 1970-01-01
  • 2019-05-08
  • 2016-03-18
  • 2017-04-29
  • 1970-01-01
相关资源
最近更新 更多