I'm trying to generate a list of Orgs who have or haven't confirmed The Watchlist like this:
| id | watchlist_id | org_name | is_confirm | created_at |
|---|---|---|---|---|
| 1 | 1 | Bank A | Yes | timestamp |
| 2 | 1 | Bank B | - | - |
| 3 | 1 | Bank C | - | - |
| 4 | 1 | Bank D | - | - |
| 5 | 2 | Bank A | - | - |
| 6 | 2 | Bank B | - | - |
| 7 | 2 | Bank C | - | - |
| 8 | 2 | Bank D | - | - |
Here are my tables:
Watchlist Table:
| id | title |
|---|---|
| 1 | Watchlist 1 |
| 2 | Watchlist 2 |
Watchlist_Confirmations Table :
| id | watchlist_id | user_id | is_confirm | created_at |
|---|---|---|---|---|
| 1 | 1 | 1 | Yes | timestamp |
Users Table :
| id | name | orgs_id |
|---|---|---|
| 1 | Alfa | 1 |
| 2 | Bravo | 2 |
| 3 | Charlie | 3 |
| 4 | Delta | 4 |
| 5 | Echo | 1 |
Organizations Table :
| id | name |
|---|---|
| 1 | Bank A |
| 2 | Bank B |
| 3 | Bank C |
| 4 | Bank D |
In my Watchlist_Confirmations table, I limit the organization to only one confirmation per watchlist. So if the Org has more than one user, that user cannot confirm on the same watchlist.
Here are my code so far. The problem is when Org have more than one user, those Org will appears twice in my table.
QueryBuilder::for(Watchlists::query()
->crossJoin('users')
->leftJoin(
'watchlist_confirmations',
function ($join) {
$join->on(
'watchlists.id',
'watchlist_confirmations.watchlists_id',
)->on(
'users.id',
'watchlist_confirmations.user_id',
);
}
)
->leftJoin(
'organizations',
'users.orgs_id',
'organizations.id',
)
->select(
'watchlists.id',
'organizations.name AS org_name',
'watchlist_confirmations.is_confirm',
'watchlist_confirmations.created_at'
)
->distinct()