我有两个表SCHOLAR和MEMBER表。我想显示Scholar表中没有scholar_id在Member表中找到的学者名单。但是我的第二个dd的结果是空的,实际上它有数据库中的数据。我做错了什么?我认为我的代码很好。检索另一个表中不存在的数据
public function list()
{
$scholars = Member::all();
$scholar_ids = [];
foreach ($scholars as $scholar) {
array_push($scholar_ids, $scholar->scholar_id);
}
$scholar_exits = Scholar::where('scholar_id','=', $scholar_ids)->get();
<!-- First -->
dd($scholar_ids);
<!-- Second -->
dd($scholar_exits);
}
<!-- First dd result -->
array:7 [▼
0 => 7
1 => 8
2 => 12
3 => 13
4 => 14
5 => 15
6 => 16
]
<!-- Second dd result -->
Collection {#275 ▼
#items: []
}
希望任何人都可以帮助我。
1)我可能会建议你使用'array_pluck'代替或'foreach'和'array_push'。 2)我猜'学者:: whereIn('id',$ scholar_ids) - > get();'应该这样做。 – Wistar