我有如下查询:
Relation1::with('relation2')
->select('relation_1_columns')
->where('relation_2_column', $relation2->id) //Throws unknown column error
->where('relation_2_column', 1) //Throws unknown column error
->get()->toArray();
如果我这样做:
Relation1::with('relation2')
->select('relation_1_columns')
->where('relation2.relation_2_column1', $relation2->id) //Throws unknown column `relation2.relation_2_column` error
->where('relation2.relation_2_column2', 1) //Throws unknown column `relation2.relation_2_column` error
->get()->toArray();
如何使用 where 子句
对使用 with
附加的表进行检查
最佳答案
使用whereHas() :
Relation1::with('relation2')
->select('relation_1_columns')
->whereHas('relation2', function($q) use ($relation2){
$q->where('relation_2_column1', $relation2->id)->where('relation_2_column2', 1);
})
->toArray();
关于mysql - 从 table1 检索数据但在 table2 中进行比较 - Laravel Eloquent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48851361/