Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customizing through relations local keys #89

Merged
merged 7 commits into from
Nov 27, 2023

Conversation

muhammedkamel
Copy link
Contributor

There is no way to customize the local keys of the through relations.

Imagine the following scenario:

Model A

  • HasMany B

Model C

  • HasOne B

Model B

  • BelongsTo C
  • BelongsTo B

belongs-to-through

From Model C I need to access Model A using belongsToThrough relation using B.c_id and B.a_id

The new implementation should enable this using the following

<?php

class C extends Model
{
    public function a(): BelongsToThrough
    {
        return $this->belongsToThrough(
            A::class,
            B::class,
            foreignKeyLookup: [B::class => 'id'],
            localKeyLookup: [B::class => 'c_id'],
        );
    }
}

and using this query

select "A".* from "A" inner join "B" on "B"."a_id" = "A"."id" where "B"."c_id" = 416 limit 1

@muhammedkamel muhammedkamel changed the title Allow local keys lookup Allow customizing through relations local keys Oct 20, 2023
@staudenmeir
Copy link
Owner

Hi @muhammedkamel,
Thanks, I'll take a look.

@muhammedkamel
Copy link
Contributor Author

muhammedkamel commented Oct 29, 2023

@staudenmeir any feedback?

@staudenmeir staudenmeir merged commit 6f5c0ce into staudenmeir:master Nov 27, 2023
7 of 8 checks passed
@staudenmeir
Copy link
Owner

Thanks! I released a new version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants