You can do
$companyUsers = CompanyUserProduct::with('companyUser:id,user_id')->get();
To retrieve the CompanyUserProduct
along with the id
and user_id
of compantUser
.
Note: whatever your primary key
for companyUser
- make sure you select it as well. (In this case assumed to be id
)
Once you retrieve the collection, the user_id
will be under companyUser
for each CompanyUserProduct
So, you can pluck it using dot notation.
eg:
$userIds = $companyUsers->pluck('companyUser.user_id');