staudenmeir / belongs-to-through

Laravel Eloquent BelongsToThrough relationships
MIT License
1.15k stars 88 forks source link

Doesn't seem to be working... #67

Closed nigelatdas closed 2 years ago

nigelatdas commented 2 years ago

We have a relationship Report -> UserData -> User with keys like this:

[   | Report | user_data_id ] ---> [ id | UserData | user_id ] ---> [ id | User |  ] 

Report has a method as follows:

    public function user()
    {
        // Farm >> UserData >> user
        return $this->hasOneThrough(User::class, UserData::class, 'id', 'id', 'user_data_id', 'user_id');
        //return $this->belongsToThrough(User::class, UserData::class, "user_data_id",'',[User::class =>"user_id"]);
        //return $this->belongsToThrough(User::class, UserData::class);
    }

If I use hasOneThrough, when the query is evaluated:

$items = $query->get($projection);

and I add a watch on $items[0]->user it evaluates to a user object.

When I use belongsToThrough it evaluates to null.

Any thoughts? Also.. is there any downside to using hasOneThrough? different behaviour on delete?

Cheers Nigel

staudenmeir commented 2 years ago

Also.. is there any downside to using hasOneThrough? different behaviour on delete?

There's no downside (the package is older than HasOneThrough).

This relationship would work (Laravel tries to use user_datum_id by default):

public function users()
{
    return $this->belongsToThrough(User::class, UserData::class, null, '', [UserData::class => 'user_data_id']);
}
nigelatdas commented 2 years ago

LOL... that makes so much sense! Perfect

On Tue, 8 Feb 2022 at 06:15, Jonas Staudenmeir @.***> wrote:

Also.. is there any downside to using hasOneThrough? different behaviour on delete?

There's no downside (the package is older than HasOneThrough).

This relationship would work (Laravel tries to use user_datum_id by default):

public function users() { return $this->belongsToThrough(User::class, UserData::class, null, '', [UserData::class => 'user_data_id']); }

— Reply to this email directly, view it on GitHub https://github.com/staudenmeir/belongs-to-through/issues/67#issuecomment-1031823589, or unsubscribe https://github.com/notifications/unsubscribe-auth/AV3WT6GEZVQBFCVY7WPJBXTU2AK3VANCNFSM5NW7BXSQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>