【发布时间】:2020-06-22 23:34:44
【问题描述】:
我试图弄清楚如何使这个查询工作。这两个表没有直接关系(即 hasOne、hasMany 等)。我希望只从client_vendor_relationship 取回没有collection_opt_in 的记录。由于我没有办法做->whereDoesntHave(),我不确定如何取回这些数据。
client_vendor_relationship
| id | client_id | vendor_id | active |
|----|-----------|-----------|--------|
| 1 | 23484 | 1872 | 1 |
| 2 | 5643 | 345 | 1 |
| 3 | 431 | 4443 | 1 |
collection_opt_in
| id | client_id | vendor_id | year |
|----|-----------|-----------|------|
| 1 | 23484 | 23484 | 2020 |
| 2 | 23484 | 23484 | 2019 |
| 3 | 431 | 4443 | 2019 |
当前查询
$relationships = ClietVendorRelationship::where('active', 1)
->leftJoin('collection_opt_in', function($join){
$join->on('client_vendor_relationship.client_id', '=', 'collection_opt_in.client_id')
->on('client_vendor_relationship.vendor_id', '=', 'collection_opt_in.vendor_id');
})
->where() // Not sure what to put here to ONLY get rows back that the left join didnt find entries for
->groupBy('client_vendor_relationship.id')
->get();
上述查询的最终目标是只从client_vendor_relationship 获取ID 为2 的行。我需要在 Eloquent 中执行此操作。我知道我可以轻松地做一个集合过滤器,但我使用的前端表需要返回一个雄辩的查询。
【问题讨论】: