按Laravel中数组内的值对记录进行分组

时间:2015-12-05 10:03:13

标签: php arrays laravel laravel-4 eloquent

我创建了一个查询,如下所示:

$countVideosWatchedThisMonth = UserVideoView::with('video')->whereBetween('created_at', array($currentMonthStart, $currentMonthEnd))->orderBy('view_count', 'DESC')->get();

这产生了数据库的以下输出。

array (size=3)
  0 => 
    array (size=7)
      'id' => int 2
      'video_id' => int 11
      'view_count' => int 42
      'created_at' => string '2015-12-01 13:00:00' (length=19)
      'updated_at' => string '2015-12-01 13:00:00' (length=19)
      'deleted_at' => null
      'video' => 
        array (size=19)
          'id' => int 11
          'language_id' => int 1
          'gradesubject_category_topic_id' => int 87
          'tutor_id' => int 19
          'title' => string 'Acceleration' (length=12)
          'vimeo_id' => string '117574716' (length=9)
          'vimeo_thumbnail_large_url' => string 'https://i.vimeocdn.com/video/504322261_295x166.jpg' (length=50)
          'description' => string 'This video explains what acceleration is. It follows the earlier videos on distance and velocity. The intention is to give the learner a more indepth pictorial view of acceleration. ' (length=182)
          'upload_status' => string 'completed' (length=9)
          'count_purchased' => int 5
          'count_rated' => int 0
          'count_watched' => int 15
          'rating_average' => null
          'status' => string 'enabled' (length=7)
          'published_at' => string '2015-01-23 13:10:44' (length=19)
          'created_at' => string '2015-01-23 10:17:36' (length=19)
          'updated_at' => string '2015-09-21 09:35:43' (length=19)
          'deleted_at' => null
          'slug' => string '11-acceleration' (length=15)
  1 => 
    array (size=7)
      'id' => int 1
      'video_id' => int 10
      'view_count' => int 20
      'created_at' => string '2015-12-04 13:00:00' (length=19)
      'updated_at' => string '2015-12-04 13:00:00' (length=19)
      'deleted_at' => null
      'video' => 
        array (size=19)
          'id' => int 10
          'language_id' => int 1
          'gradesubject_category_topic_id' => int 83
          'tutor_id' => int 19
          'title' => string 'Introduction to Acids & Bases ' (length=30)
          'vimeo_id' => string '117574110' (length=9)
          'vimeo_thumbnail_large_url' => string 'https://i.vimeocdn.com/video/504321450_295x166.jpg' (length=50)
          'description' => string 'Introduction to Acids and Bases' (length=31)
          'upload_status' => string 'completed' (length=9)
          'count_purchased' => int 1
          'count_rated' => int 0
          'count_watched' => int 1
          'rating_average' => null
          'status' => string 'enabled' (length=7)
          'published_at' => string '2015-01-23 13:45:40' (length=19)
          'created_at' => string '2015-01-23 09:43:20' (length=19)
          'updated_at' => string '2015-11-07 09:01:09' (length=19)
          'deleted_at' => null
          'slug' => string '10-introduction-to-acids--bases-' (length=32)
  2 => 
    array (size=7)
      'id' => int 4
      'video_id' => int 47
      'view_count' => int 10
      'created_at' => string '2015-12-01 13:00:00' (length=19)
      'updated_at' => string '2015-12-01 13:00:00' (length=19)
      'deleted_at' => null
      'video' => 
        array (size=19)
          'id' => int 47
          'language_id' => int 1
          'gradesubject_category_topic_id' => int 87
          'tutor_id' => int 19
          'title' => string 'Vectors' (length=7)
          'vimeo_id' => string '118220963' (length=9)
          'vimeo_thumbnail_large_url' => string 'https://i.vimeocdn.com/video/505197719_295x166.jpg' (length=50)
          'description' => string 'Defining vectors' (length=16)
          'upload_status' => string 'completed' (length=9)
          'count_purchased' => int 0
          'count_rated' => int 0
          'count_watched' => int 0
          'rating_average' => null
          'status' => string 'enabled' (length=7)
          'published_at' => string '2015-01-30 10:43:44' (length=19)
          'created_at' => string '2015-01-30 07:36:34' (length=19)
          'updated_at' => string '2015-02-02 07:24:25' (length=19)
          'deleted_at' => null
          'slug' => string '47-vectors' (length=10)

目前有3条记录,我只需要通过'gradesubject_category_topic_id'检索顶部值,该值位于数组中。

我已经完成了orderBy()函数,以便只检索第一个唯一值。

但是,我现在无法仅使用唯一的'gradesubject_category_topic_id'

来检索顶部值

在这种情况下,输出只会检索前两个数组元素,因为最后一个数组元素与数组中的第一条记录具有相同的'gradesubject_category_topic_id'

我希望这是有道理的。非常感激。

1 个答案:

答案 0 :(得分:1)

您可以通过加入这两个表来完成此操作。

$countVideosWatchedThisMonth = DB::table('user_video_view')
    ->join('video','user_video_view.video_id','=','video.id')
    ->whereBetween('created_at', array($currentMonthStart,$currentMonthEnd))
    ->orderBy('view_count', 'DESC')
    ->orderBt('gradesubject_category_topic_id','desc')
    ->first()

或试试这个,

$countVideosWatchedThisMonth = DB::table('user_video_view')
    ->join('video','user_video_view.video_id','='.'video.id')
    ->whereBetween('created_at', array($currentMonthStart,$currentMonthEnd))
    ->orderBy('view_count', 'DESC')
    ->max('gradesubject_category_topic_id')