Skip to content

Instantly share code, notes, and snippets.

@tangoslee
Last active January 11, 2023 02:53
Show Gist options
  • Save tangoslee/9182f7194e77f9fbe8bdcb72a1f9111a to your computer and use it in GitHub Desktop.
Save tangoslee/9182f7194e77f9fbe8bdcb72a1f9111a to your computer and use it in GitHub Desktop.
Laravel relation with composite key
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