2017-08-02 52 views
0

我想知道如何将这个查询转换为Laravel?将一个复杂的查询转换成Laravel的雄辩

SELECT * FROM users INNER JOIN srp_user_statistics ON users.id = srp_user_statistics.user_id ORDER BY srp_user_statistics.payslips_collected DESC LIMIT 2 

“用户”表是在一个叫播放器模式和srp_user_statistics表是在一个称为角色扮演模式被称为模态球员的关系“角色扮演”

我想这一点,但它的返回DESC选择ASC即便:

$players = Player::whereHas('roleplay', function ($query) use($orderType) { 
    $query->orderBy('payslips_collected', $orderType); 
})->get(); 
+0

这是同样的问题https://stackoverflow.com/questions/45459125/laravel-orderby-not-working/45459529#45459529我只是给你这个解决方案? –

+0

对不起?这不是同一个问题 –

回答

0

使用Larvel查询生成器:

$record = DB::table('users') 
->innerJoin('srp_user_statistics', 'users.id', '=', 'srp_user_statistics.user_id') 
->orderBy('payslips_collected', 'DESC') 
->limit(2) 
->get(); 
+0

嗨,我收到以下错误? (1/1)BadMethodCallException调用未定义的方法在Builder中调用\ Database \ Query \ Builder :: innerJoin()。 –

+0

@JoshHallow尝试只使用“连接”而不是“innerJoin”。 –

+0

是的,尝试使用连接。 –