Laravel Query Builder MIN方法

时间:2015-04-03 12:35:04

标签: php laravel laravel-5 min

我在下面的代码(Laravel 5 Query Builder)中应该返回id在指定id和最小id之下的帖子列表。

如您所见,查询使用take来限制结果,传递的值是一个整数。

问题是postList变量正确包含请求的值,但firstPostID返回db中建立的最小值,而不是我希望获得的查询的最小值。

$q = DB::table('posts')
            ->select('posts.id', 'posts.title', 'posts.created_at', 'users.name AS creatorName')
                ->join('users', 'users.id', '=', 'posts.creatorid_ext')
                ->take(Config::get('generals.history_posts_number'))
                ->where('posts.id', '<', $id)->orderBy('posts.id', 'desc');



        $postsList   = $q->get();

        $firstPostID = $q->min('posts.id');

这就是我得到的

{
   "firstid": 1,
   "posts": [
      {
         "created_at": "2015-03-01 12:00:00",
         "creatorName": "XXXX",
         "id": 6,
         "title": "this is the sixth title"
      },
      {
         "created_at": "2015-03-01 11:00:00",
         "creatorName": "XXX",
         "id": 5,
         "title": "this is the fifth title"
      },
      {
         "created_at": "2015-03-01 10:00:00",
         "creatorName": "XXX",
         "id": 4,
         "title": "this is the fourth title"
      },
      {
         "created_at": "2015-03-01 09:00:00",
         "creatorName": "XXX",
         "id": 3,
         "title": "this is the tirhd title"
      }
   ]
}

这就是我想要的

{
   "firstid": 3,
   "posts": [
      {
         "created_at": "2015-03-01 12:00:00",
         "creatorName": "XXX",
         "id": 6,
         "title": "this is the sixth title"
      },
      {
         "created_at": "2015-03-01 11:00:00",
         "creatorName": "XXX",
         "id": 5,
         "title": "this is the fifth title"
      },
      {
         "created_at": "2015-03-01 10:00:00",
         "creatorName": "XXX",
         "id": 4,
         "title": "this is the fourth title"
      },
      {
         "created_at": "2015-03-01 09:00:00",
         "creatorName": "XXX",
         "id": 3,
         "title": "this is the tirhd title"
      }
   ]
}

非常感谢您的帮助

0 个答案:

没有答案