Laravel Eloquent 中的 MySQL 查询

标签 mysql laravel laravel-4 eloquent

我如何在 Eloquent 中做同样的事情(即 Model::where(...)->join(...)?:

DB::select('SELECT users.id, users.username
        FROM users, teams, teams AS teams2
        WHERE users.team_id = teams2.id
        AND teams.id = ?
        AND teams2.l BETWEEN teams.l AND teams.r', [$id])

最佳答案

编辑:

$team = Team::find($id);
$users = User::join('teams', 'teams.id', '=', 'users.team_id')
             ->whereBetween('teams.l', [$team->l, $team->r])
             ->get(['users.id', 'users.username']);

关于Laravel Eloquent 中的 MySQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41714718/

相关文章:

PHP print_r 清除 mysqli 连接对象的状态?

php - MySQL存储 '\abc\def\fg.jpg'问题

laravel - 删除然后在当前向上迁移功能中创建相同的列

unit-testing - 断言返回值是 Redirect 还是 View

MySQL:计算每天每小时的平均帖子

php - 将 MySQL 列名映射到变量名

php - Laravel 查询生成器 - 通过修改后的 where 语句重用查询

php - 如何在 Laravel 5.3 中创建两次连接到表的查询?

未创建 laravel services.json

javascript - Laravel 4 中的 Jquery Ajax - NotFoundHttpException