使用 Eloquent 基于 Table B 从 Table A 检索数据

Retrieve data from Table A based on Table B using Eloquent

我有两个 table:requestgenerals 和 requestinformations。 2个table之间的关系是:

以下是 table: 请求将军 table

和 请求信息 table

我尝试了以下操作:$requestgenerals = Requestgeneral::without('requestinformation')->get(); 但我仍然从 requestgenerals table 获取所有行,而不仅仅是 two.Please assist

你应该使用这个:

$requestgenerals = Requestgeneral::doesntHave('requestinformations')->get();

如果你想获取所有在秒table中没有相关记录的记录。看看Eloquent documentation.

使用doesntHave获取数据没有requestinformations

$requestgenerals = Requestgeneral::doesntHave('requestinformations')->get();

参考这个 link

Example