Last active
October 2, 2020 14:09
-
-
Save mikehins/efa9ae0c8d944d5a16f1591afc76be05 to your computer and use it in GitHub Desktop.
Visual aid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.laravelhasmanythrough.com/ | |
public function offices() | |
{ | |
/* | |
* SQL Query | |
* | |
SELECT | |
`offices`.*, | |
`office_user`.`user_id` ($1) | |
FROM | |
`offices` | |
INNER JOIN `office_user` ON `office_user`.`office_id` ($4) = `offices`.`id` ($2) | |
WHERE | |
`office_user`.`user_id` = 1001119 ($3) | |
ORDER BY offices.`code` | |
* */ | |
return $this->hasManyThrough(Office::class, OfficeUser::class, | |
'user_id', // $1 - SELECT ... `office_user`.`user_id` | |
'id', // $2 - ON ... `offices`.`id` | |
'user_id', // $3 - WHERE `office_user`.`office_id` = 1001119 | |
'office_id' // $4 - ON `office_user`.`office_id` ... | |
)->selectRaw('offices.*, CONCAT("#", code, " - ", name) as name') | |
->orderBy('offices.code'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment