mysql - 在两个内连接表中使用 WHERE 的 Eloquent ORM

标签 mysql orm laravel eloquent

我正在尝试从两个表中获取结果,我需要从这两个表中过滤出我需要的信息类型。到目前为止我所拥有的是:

// get list of students in this class; 
$students = DB::table('students')
->join('userinfo', 'students.studentUserID', '=', 'userinfo.userinfoUserID')
->select('userinfo.userinfoInfo', 'userinfo.userinfoUserID')
->where('students.studentClassID', '=', $cid)
->get(); 

这工作正常,但我想进一步过滤结果。 我拥有用户信息列的方式是这样的:

id | userinfo.UserID | userinfo.userinfoType | userinfo.userinfoInfo 2 | 3 | firstName | Johnny 3 | 3 | lastName | Baker 4 | 3 | phone | 5551234543

我只想要名字信息。所以像这样:

->where('userinfo.userinfoType', '=', 'firstName')

如何在 Eloquent 中运行这样的查询?我正在使用拉拉维尔。

最佳答案

您可以使用 QueryBuilder 执行类似的操作:

$students = DB::table('students')
    ->join('userinfo', function($join)
        {
            $join->on('students.studentUserID', '=', 'userinfo.userinfoUserID')
                ->orOn('userinfo.userinfoType', '=', 'firstName')
        })
    ->select('userinfo.userinfoInfo', 'userinfo.userinfoUserID')
    ->where('students.studentClassID', '=', $cid)
    ->get(); 

关于mysql - 在两个内连接表中使用 WHERE 的 Eloquent ORM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18599455/

相关文章:

php结果返回空

c# - 使用哪个ORM?

python - 如何使用 Django ORM 排除列的多个值?

php - Laravel 5 - 更改模型文件位置

php - Laravel Horizo​​n - 有不同守卫的门

php - 在检索 MySQL 表时克服 PHP 内存耗尽或执行时间错误

php - Laravel Session 检测到一个域、多个数据库

mysql - sql order by 子句中的多种情况条件?

php - Doctrine alter table 导致 auto_increment 重新排序

php - Laravel 容器和共享实例