Laravel Eloquent AS关键字 [英] Laravel Eloquent AS keyword
本文介绍了Laravel Eloquent AS关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用关键字AS组合两列,以便可以对该列进行排序.
I'm trying to use the keyword AS to combine two columns so I can sort on that column.
这是目前的完整查询.
$quotes = Quote::where('created_at', '>=', $date)
->where('created_at', '<=', date('Y-m-d').' 23:59:59')
->order_by('upvotes', 'desc')
->paginate(5);
我想做
->order_by('(downvotes - upvotes) as votes', 'desc')
谢谢.
似乎使用DB :: raw()是唯一的方法,Laravel/Eloquent只是不了解AS.
Seems as though using DB::raw() is the only way to do it, Laravel/Eloquent just doesn't understand the AS.
有效的解决方案是
$quotes = Quote::select(array('id', DB::raw('(downvotes - upvotes) as votes'), 'upvotes', 'downvotes', 'etc')) // Rest of column needed.
->where('created_at', '>=', $date)
->where('created_at', '<=', date('Y-m-d').' 23:59:59')
->order_by('votes', 'asc')
->paginate(5);
推荐答案
尝试一下:
Quote::where('created_at', '>=', $date)
->select(array('id',DB::raw('(downvotes - upvotes) as votes'))) //and what you need
->where('created_at', '<=', date('Y-m-d').' 23:59:59')
->order_by('upvotes', 'desc')
->order_by('votes', 'desc')
->paginate(5);
这篇关于Laravel Eloquent AS关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文