Skip to content

Instantly share code, notes, and snippets.

@mikehins
Last active October 2, 2020 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikehins/efa9ae0c8d944d5a16f1591afc76be05 to your computer and use it in GitHub Desktop.
Save mikehins/efa9ae0c8d944d5a16f1591afc76be05 to your computer and use it in GitHub Desktop.
Visual aid
// 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