Last active
January 11, 2023 02:53
-
-
Save tangoslee/9182f7194e77f9fbe8bdcb72a1f9111a to your computer and use it in GitHub Desktop.
Laravel relation with composite key
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
public function account(): BelongsTo | |
{ | |
return new BelongsTo( | |
Account::where('owner_id', $this->owner_id), | |
$this, | |
'owner_type', | |
'owner_type', | |
'BelongsToWithCompositeKey' | |
); | |
} | |
public function user(): BelongsTo | |
{ | |
return new BelongsTo( | |
Account::where('owner_id', $this->owner_id) | |
->join('users', 'accounts.user_id', '=', 'users.id') | |
->selectRaw('users.*'), | |
$this, | |
'owner_type', | |
'owner_type', | |
'User' | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment