doujia9204 2017-04-13 11:11
浏览 35
已采纳

在laravel中显示每周的每一天的帖子计数

I have a function in my controller with this:

$one_week_ago = Carbon::now()->subDays(6)->format('Y-m-d');
$dates = Post::where('created_at', '>=', $one_week_ago)
        ->groupBy('date')
        ->orderBy('date', 'ASC')
        ->get(array(
             DB::raw('Date(created_at) as date'),
             DB::raw('COUNT(*) as "count"')
          ));

return view ('analytics', array('dates' => $dates));

And my view is:

@foreach ($dates as $date => $count) 
      <p> {{ $date . '->' . $count }}</p>
@endforeach

I am getting the output as:

0 -> {"date":"2017-04-07","count":7}

1 -> {"date":"2017-04-08","count":2}

2 -> {"date":"2017-04-12","count":4}

3 -> {"date":"2017-04-13","count":1}

Problem: The dates in which has no posts (i.e postscount = 0) those are not shown. Example: 2017-04-09, 2017-04-10 and 2017-04-11 are not shown.

But I want the output for those dates as count:0

  • 写回答

3条回答 默认 最新

  • dongxuanyi3406 2017-04-13 12:34
    关注

    One approach to tackle this is to build a collection containing the default values first (keys are the dates you're looking at, and the values are 0). This gives you an array that looks like this:

    [
        '2017-04-07' => 0,
        '2017-04-08' => 0,
        '2017-04-09' => 0,
        '2017-04-10' => 0,
        '2017-04-11' => 0,
        '2017-04-12' => 0,
        '2017-04-13' => 0,
    ]
    

    Then we take the posts we found and call pluck - this lets us specify the value we want (count) and the key we'd like to use (date). This won't necessarily have all of the days we want in it, and will look something like this:

    [
        '2017-04-07' => 12,
        '2017-04-10' => 3,
        '2017-04-11' => 7,
    ]
    

    Finally, we merge the two based on the keys, using the first collection as the base. This means we get to keep all the default values we set up (and more importantly, all 7 dates we set) and it just overwrites individual day values with the results from the database.


    Here's the final code:

    // Build an array of the dates we want to show, oldest first
    $dates = collect();
    foreach( range( -6, 0 ) AS $i ) {
        $date = Carbon::now()->addDays( $i )->format( 'Y-m-d' );
        $dates->put( $date, 0);
    }
    
    // Get the post counts
    $posts = Post::where( 'created_at', '>=', $dates->keys()->first() )
                 ->groupBy( 'date' )
                 ->orderBy( 'date' )
                 ->get( [
                     DB::raw( 'DATE( created_at ) as date' ),
                     DB::raw( 'COUNT( * ) as "count"' )
                 ] )
                 ->pluck( 'count', 'date' );
    
    // Merge the two collections; any results in `$posts` will overwrite the zero-value in `$dates`
    $dates = $dates->merge( $posts );
    
    return view( 'analytics', compact( 'dates' ) );
    

    Your view then should simply be

    @foreach ( $dates as $date => $count ) 
        <p> {{ $date }} = {{ $count }}</p>
    @endforeach
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题