3

キーワード AS を使用して 2 つの列を結合しようとしているので、その列で並べ替えることができます。

これは現時点での完全なクエリです。

$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 を理解していません。

作業ソリューションは

 $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);
4

2 に答える 2

2

これを試して:

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);
于 2013-03-01T13:31:54.340 に答える
0
$selectdata = DB::table('TableDetails')
            ->leftJoin('TableStatus', 'TableDetails.TableID', '=', 'TableStatus.TableID')
            ->leftJoin('LoginUser', 'TableStatus.WaiterLoginID', '=', 'LoginUser.LoginID')
            ->leftJoin('UserRole', 'LoginUser.UserRoleID', '=', 'UserRole.UserRoleID')
            ->select('LoginUser.UserName','UserRole.UserRoleName','TableStatus.TableStatus','TableStatus.TableTimeStatus','TableStatus.WaiterLoginID','TableDetails.TableSeatCount - TableStatus.TableSeatOccupied AS TableRemainingCount')
            ->get();

マイナス記号の前後に「スペース」を置くだけでうまくいきました。

于 2015-06-16T04:48:36.680 に答える