staudenmeir / belongs-to-through

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

Existing Relationships like belongsToThroughFromRelations() #63

Closed 0xTushar closed 2 years ago

0xTushar commented 2 years ago

can I do reverse of hasManyDeepFromRelations as belongsToThroughFromRelations?

0xTushar commented 2 years ago

The environment scenario is like below

On User model


    // for deep relations 
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
    use \Znck\Eloquent\Traits\HasTableAlias;

 public function myTeams()
    {
        return $this->hasMany(User::class, 'teamleader_id', 'id');
    }

    public function myLeader() {
        return $this->belongsTo(User::class, 'teamleader_id');

    }
    public function userTransactions()
    {
        return $this->hasMany(Transaction::class, 'user_id', 'id');
    }

     // deep relationship

    public function myTeamTransactions()
    {
        return $this->hasManyDeepFromRelations($this->myTeams(), $this->userTransactions());
    }
Now Problem is getting on the Transaction Model 

transaction owned by a user has a leader need to pull That leader Data. How do I do That with belongsToThrough
    public function leader()
    {
        return $this->belongsToThrough(
            User::class,
             'App\Models\User as team',
        );
    }

Getting Error with `SQLSTATE[42S22]: Column not found: 1054 Unknown column 'team.user_id' in 'on clause' (SQL: select `users`.*, `team`.`id` as `laravel_through_key` from `users` inner join `users` as `team` on `team`.`user_id` = `users`.`id` where `team`.`deleted_at` is null and `team`.`id` in (21, 47, 51, 52, 78, 157, 204, 206, 212, 259, 260, 264, 267, 280, 294, 301, 302, 310, 312, 314, 320, 331, 348, 349, 405, 414, 422, 588, 591, 592) and `users`.`deleted_at` is null)`

When I'm using

 public function leader()
    {
        return $this->belongsToThrough(
            User::class,
            'App\Models\User as team',
            null,
            '',
            [User::class => 'teamleader_id'],
        );
    } `  its returned null 

after debugging with laravel debug bar get this SQL

 `select `users`.*, `team`.`id` as `laravel_through_key` from `users` inner join `users` as `team` on `team`.`teamleader_id` = `users`.`id` where `team`.`deleted_at` is null and `team`.`id` in ('') and `users`.`deleted_at` is null

/vendor/staudenmeir/belongs-to-through/src/Relations/BelongsToThrough.php:248`

staudenmeir commented 2 years ago

team.id in ('')

Does your SQL actually contain in ('') or did you change that for the post?

How are you using the relationship in your eager loading query?

0xTushar commented 2 years ago

yes it contains in ('') only

image

for debugging purposes, I'm trying now with this code


   \Debugbar::info(Transaction::take(10)->latest()->with('leader')->get());
0xTushar commented 2 years ago

Relationship Scenario is.

Leader → has many → User → has many → Transaction

Transaction → belongs to → User → belongs to → Leader (same User model with foreign key )

staudenmeir commented 2 years ago

What's the result of dd(Transaction::take(10)->latest()->first()->getAttributes());?

0xTushar commented 2 years ago

image

staudenmeir commented 2 years ago

I looked into it: The package actually doesn't support this special case yet, but you can define the relationship with the eloquent-has-many-deep package you are already using:

class Transaction extends Model
{
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

    public function leader()
    {
        return $this->hasOneDeep(
            User::class,
            [User::class . ' as team'],
            ['id', 'id'],
            ['user_id', 'teamleader_id']
        );
    }
}