首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >过滤器表,基于另一个表中的字段

过滤器表,基于另一个表中的字段
EN

Stack Overflow用户
提问于 2019-03-09 15:37:37
回答 1查看 141关注 0票数 0

这件事让我很困惑。我有一张包含各种字段的表:$employees。我猜,这就是您所称的集合,我认为,当我调用时,会返回数据库中的所有员工记录(本例中有4条记录)。

每个员工记录都有以下字段first_namelast_nameageother_id

还有另一个表(或集合),我称之为filter表。它被称为$other_ids。这有两个记录,包含以下字段-- idid_name

我希望能够过滤$employees表,以便它只保存记录,其中other_id等于筛选表中id的两个值之一- $other_ids

因此,例如,如果过滤器表有以下两条记录:

代码语言:javascript
复制
[{"id":1 "id_name":"one"}, {"id":2, "id_name":"two"}]

$employee表包含以下记录:

代码语言:javascript
复制
[{"first_name":"ted", "surname_name":"stark", "age":35, "other_id":1},
{"first_name":"fred", "surname_name":"strange", "age":30, "other_id":2},
{"first_name":"incredible", "surname_name":"hulk", "age":25, "other_id":3},
{"first_name":"captain", "surname_name":"stone", "age":28, "other_id":2}]

在过滤之后,它应该返回$employees_filtered,应该只有记录1、2和4

我尝试过执行left-join和使用whereHas,以及where子句,但是没有任何效果!

EN

回答 1

Stack Overflow用户

发布于 2019-03-09 17:31:17

我想你是在找-

代码语言:javascript
复制
$otherId = [1, 2];
$employees_filtered = Employee::with('Others')->whereIn('other_id', $otherId)->get();

请别忘了和他们的模特建立关系。在Other.php模型中-

代码语言:javascript
复制
public function Employees()
{
    return $this->hasMany('App\Other', 'other_id', 'id');
}

Employee.php模型中-

代码语言:javascript
复制
public function Others()
{
    return $this->belongsTo('App\Employee', 'other_id', 'id');
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55078953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档