staudenmeir / eloquent-has-many-deep

Laravel Eloquent HasManyThrough relationships with unlimited levels
MIT License
2.67k stars 157 forks source link

RetrievesIntermediateTables::withPivot Enhancement #164

Closed kaydenvanrijn closed 1 year ago

kaydenvanrijn commented 2 years ago

https://github.com/staudenmeir/eloquent-has-many-deep/blob/81d2d7304949416d6f5a4ab6d00cc07524d07129/src/RetrievesIntermediateTables.php#L45

Feature request to match Eloquent:

  1. By default, the pivot accessor should be 'pivot', rather than the model's name
  2. Instead of having to pass both the table and the class, one should only have to pass a pivot parameter which would be the class name (e.g UserContact::class)
    • The func would automatically resolve the table name using something like (new Pivot())->getTable()

Here's the current way to do it:

public function primaryContact (): HasOneDeep
{
    $userContactsTable = (new UserContact())->getTable();

    return $this->hasOneDeep(Contact::class, [$userContactsTable])->withPivot(
        table: $userContactsTable,
        class: UserContact::class,
        accessor: 'pivot'
    );
}

Here's my requested way of doing it:

public function primaryContact (): HasOneDeep
{
    return $this->hasOneDeep(Contact::class, [UserContact::class])->withPivot(
        pivot: UserContact::class,
        columns: ['*'],
    );
}
staudenmeir commented 2 years ago

I'll take a look.

staudenmeir commented 1 year ago

By default, the pivot accessor should be 'pivot', rather than the model's name

This would be a breaking change for existing users.

Instead of having to pass both the table and the class, one should only have to pass a pivot parameter which would be the class name (e.g UserContact::class)

That's what withIntermediate()does. withPivot() is the generic method that also supports cases where it can't resolve the table name from the class.