Laravel 3 - 由两个领域划分

时间:2013-08-04 04:18:15

标签: php laravel eloquent laravel-3

我需要将以下SQL转换为雄辩

select * from medias order by likes/views DESC, views ASC

我需要在结果上使用paginate,这就是我更喜欢雄辩的原因。

我的一些其他SQL查询是

$media_list = Media::order_by('likes', 'desc')->paginate($per_page);

我试过

$media_list = Media::order_by('likes/views', 'desc')->paginate($per_page);

但是它给出了错误

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'likes/views' in 'order clause'
SQL: SELECT * FROM `medias` ORDER BY `likes/views` DESC LIMIT 20 OFFSET 0

任何人都知道如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

尝试使用,代替/

$media_list = Media::order_by('likes,views', 'desc')->paginate($per_page);

$media_list = Media::order_by('likes`,`views', 'desc')->paginate($per_page);

这也是laravel的标准做法

$media_list = Media::order_by('likes', 'desc')->orderBy('views', 'desc')->paginate($per_page);

$media_list = DB::table('medias')
    ->select(DB::raw('(likes/views) AS resultant'))
    ->order_by('resultant', 'desc')->orderBy('views', 'desc')
    ->get();