这件事让我很困惑。我有一张包含各种字段的表:$employees。我猜,这就是您所称的集合,我认为,当我调用时,会返回数据库中的所有员工记录(本例中有4条记录)。
每个员工记录都有以下字段first_name、last_name、age、other_id
还有另一个表(或集合),我称之为filter表。它被称为$other_ids。这有两个记录,包含以下字段-- id、id_name。
我希望能够过滤$employees表,以便它只保存记录,其中other_id等于筛选表中id的两个值之一- $other_ids。
因此,例如,如果过滤器表有以下两条记录:
[{"id":1 "id_name":"one"}, {"id":2, "id_name":"two"}]$employee表包含以下记录:
[{"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子句,但是没有任何效果!
发布于 2019-03-09 17:31:17
我想你是在找-
$otherId = [1, 2];
$employees_filtered = Employee::with('Others')->whereIn('other_id', $otherId)->get();请别忘了和他们的模特建立关系。在Other.php模型中-
public function Employees()
{
return $this->hasMany('App\Other', 'other_id', 'id');
}在Employee.php模型中-
public function Others()
{
return $this->belongsTo('App\Employee', 'other_id', 'id');
}https://stackoverflow.com/questions/55078953
复制相似问题