PHP Laravel如何将日期数据分组为几个月并添加标题

时间:2019-01-09 12:23:13

标签: php laravel laravel-5

我正在构建一个小型January 11.1 - blabla more blablabla the same day 15.1 - blablabla February 05.2 - blabla etc... -app,我希望在其中显示按月分组的特定发行版。像这样:

public function index() {

  $releases = Release::orderBy('release_date', 'asc')->get();

  return view('pages.index')->with('releases', $releases);
}

所以,在我的控制器中,我只是这样做:

@foreach ($releases as $release)
     <div>
        <p>
            <span>{{ \Carbon\Carbon::parse($release->release_date)->format('d. M')}}</span>&nbsp;
            <span>{{$release->artist}} - {{$release->album_title}} </span>
        </p>
    </div>
@endforeach

然后在刀片视图文件中浏览数据:

Laravel 5.5

这为我提供了正确的数据,例如DB条目的列表,但是如前所述,我想按月份对它们进行分组,并另外为每个月添加标题。我该如何实现?

如果这很重要,我正在使用{{1}} ...

8 个答案:

答案 0 :(得分:2)

您可以这样做:

表:发行版

id    |    released_at    |    bla_details    |    month_header_id

表格:month_headers

id    |    month(specific length int)    |    year(specific length int)    |    header

型号:Release.php

public function month_header() 
{
    return $this->belongsTo('App\Models\Month');
}

型号:MonthHeader.php

public function releases() 
{
    return $this->hasMany('App\Models\Release');
}

助手:getMonthName($ integer)

控制器:TempController.php

public function temp()
{
    $response['month_headers'] = MonthHeader::all();
    return view('tempView', $response);
}

刀片视图:tempView.blade.php

@foreach($month_headers as $header)
    {{getMonthName($header->month)." (".$header->header.")"}}
    <ul>
        @foreach($month_headers->releases() as $release)
            <li>{{date('d.m', strtotime($release->released_at))." - ".$release->bla_details}}</li>
        @endforeach
    </ul>
@endforeach

查看输出

一月(Jan标头)

  • 11.1-blabla
  • 15.1-blablabla

2月(2月标题)

  • 05.2-blabla
  • 06.2-blabla

等...

答案 1 :(得分:1)

public function index() {

    $releases = Release::orderBy('release_date', 'asc')
            ->get()->groupBy('release_date.month');

    return view('pages.index')->with('releases_groups', $releases);
}

@foreach ($releases_groups as $month => $releases_group)
    // just place group info here...
    @foreach ($releases_group as $release)
    <div>
        <p>
          <span>{{ \Carbon\Carbon::parse($release->release_date)->format('d. M')}}</span>&nbsp;
          <span>{{$release->artist}} - {{$release->album_title}} </span>
        </p>
    </div>
  @endforeach
@endforeach

尝试一下。

答案 2 :(得分:1)

我建议让您的控制器做更多的工作:

public function index() {

  // Here we get a distinct list of dates in your format
  $releaseDates = Release::Selectraw('date_format(date(release_date),"%d.%m") as Date')->distinct()->get();

  // For each date we now get the elements for each date and put them as an array entry in to the date under a 'data' section
  foreach($releaseDates as $releaseDate){
     $releaseDate['data'] = Release::whereraw('date_format(date(release_date),"%d.%m") = '.$releaseDate->Date)->get();
  }

  // Pass the data to the view as before but with new name
  return view('pages.index')->with('releaseDates', $releaseDates );
}

然后在您看来:

// For each release date first
@foreach ($releaseDates as $releaseDate)
     <div>
        <p>
            //Print out the date as requested
            <span>{{ \Carbon\Carbon::parse($releaseDate->Date)->format('d. M')}}</span>&nbsp;
            // Now loop through each record nested as an array inside the date
            @foreach($releaseDate['data'] as $data)
                 <span>{{$data->artist}} - {{$data->album_title}} </span>
                 <br>
            @endforeach
        </p>
    </div>
@endforeach

答案 3 :(得分:0)

UNION ALL是将标头和ggroup日期数据分组为月的最佳方法

答案 4 :(得分:0)

您可以像这样使用Collection :: groupBy()方法:

$releases->groupBy(function(Release $release) {
     return $release->release_date->format('F');
});

现在 $ releases 是具有月份名称作为键的Collection和具有Collection(发布数组)的值,如下所示:

[
    "January" => Collection{Release, Release, Release},
    "February" => Collection{Release, Release, Release},
    ...
]

别忘了几年;)

答案 5 :(得分:0)

如果您使用-> get()-> groupBy('release_date')方法,则会按日期返回对象。

例如;

public function index() {

  $releases = Release::orderBy('release_date', 'asc')->get()->groupBy('release_date');

  return view('pages.index')->with('releases', $releases);
}

我想解决您的问题。

答案 6 :(得分:0)

那(在您的控制器中)这样的事情呢?

$groupedReleases = null;

foreach ($releases as $release) {
    $date = $release->release_date->format('your desired format');
    $groupedReleases[$date][] = $release;
}

return $groupedReleases;

答案 7 :(得分:0)

  

请参阅下面的带有原始查询的选择功能,在其中我添加了“月份名称”和“月份编号”作为参考。请检查输出。希望这对您有帮助

$releases = Release::select(DB::raw('some_field, MONTHNAME(release_date) as month_name, MONTHNAME(release_date) as month'))
                          ->orderBy('release_date', 'asc')->get();
  

如果您确定同一日期发布的版本不超过一个,则可以使用GroupBy。

    $releaseArray=[];
    if(!empty($releases)) {
        foreach($releases as $release){
            $releaseArray[$release->month][] = $release;
        }
    }

    print_r($releaseArray);

现在,您可以将$ releaseArray传递到视图并迭代按月分组的发布。