syd8906

323 经验值

vuejs2.0是基础视频还是实战性质的呢?

Log 查看的这个包我要插一下... 经常碰到处理稍大点的log文件时,页面显示都要2分钟左右,可能是因为用的国外cdn的原因吧,总之使用起来很不舒服... 推荐换成 ARCANEDEV/LogViewer[https://github.com/ARCANEDEV/LogViewer] 这个插件

哈哈,当然是 vuejs + webpack +laravel 实现的前后端分离项目咯

来圆个坑。。。

上面的查询在laravel里用whereIn 即可 如下:

Article::with('content')

        ->whereIn('id',function($query){
            $query->select(DB::raw('max(id)'))
                ->from('message')
                ->where('receive_id',5)
                ->where('type',4)
                ->orWhere('receive_id',0)
                ->groupBy('topic_id');
        })
        ->paginate($page);

@mostwin 谢谢回复,但是这样做不行,打印出的sql是
select * from article group by topic_id order by topic_id desc, id desc

仍然是先执行了group by

我有一个模型,Article 

article表数据如下,每一行对应一段数据

id = 1 , topic_id = 1 ,created_at =2016-05-28 12:22:33
id = 2 , topic_id = 1 ,created_at =2016-05-28 12:23:03
id = 3 , topic_id = 2,created_at =2016-05-28 12:23:33
id = 4 , topic_id = 3 ,created_at =2016-05-28 12:24:33
id = 5 , topic_id = 1 ,created_at =2016-05-28 13:22:33

现在想通过Article模型获取数据表里的所有数据,要求必须没有重复且是最新的

于是执行

Article::orderBy('topic_id','desc')->groupBy('topic_id')->get()

执行完成后 获取到的数据如下(id顺序请忽略)

id = 1 , topic_id = 1 ,created_at =2016-05-28 12:22:33
id = 3 , topic_id = 2,created_at =2016-05-28 12:23:33
id = 4 , topic_id = 3 ,created_at =2016-05-28 12:24:33

数据不是我想要,正确数据应该是

id = 5 , topic_id = 1 ,created_at =2016-05-28 13:22:33
id = 3 , topic_id = 2,created_at =2016-05-28 12:23:33
id = 4 , topic_id = 3 ,created_at =2016-05-28 12:24:33

结果和我想要的不同,原因可能是sql默认先执行了group by 而后再执行的order by

那么,遇到这种情况,在不写原生sql的情况下如何解决呢?

解决方法如下:

Article::with('content')
      ->whereIn('id',function($query){
           $query->select(DB::raw('max(id)'))
           ->from('message')
           ->where('receive_id',5)
           ->where('type',4)
           ->orWhere('receive_id',0)
           ->groupBy('topic_id'); 
      })
->paginate($page);