Skip to content

Commit

Permalink
Add example for pivot table alias
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Apr 7, 2019
1 parent 7466db7 commit 48e5a70
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ You can specify a custom pivot model as the third argument and a custom accessor
public function permissions()
{
return $this->hasManyDeep('App\Permission', ['role_user', 'App\Role'])
->withPivot('role_user', ['expires_at'], 'App\RoleUserPivot', 'pivot');
->withPivot('role_user', ['expires_at'], 'App\RoleUser', 'pivot');
}

foreach ($user->permissions as $permission) {
Expand Down Expand Up @@ -396,6 +396,25 @@ class Comment extends Model
}
```

For pivot tables, this requires custom models:

```php
class User extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

public function permissions()
{
return $this->hasManyDeep('App\Permission', ['App\RoleUser as alias', 'App\Role']);
}
}

class RoleUser extends Pivot
{
use \Staudenmeir\EloquentHasManyDeep\HasTableAlias;
}
```

### Soft Deleting

By default, soft-deleted intermediate models will be excluded from the result. Use `withTrashed()` to include them:
Expand Down
6 changes: 3 additions & 3 deletions tests/HasManyDeepTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Tests\Models\Comment;
use Tests\Models\Country;
use Tests\Models\Post;
use Tests\Models\RoleUserPivot;
use Tests\Models\RoleUser;
use Tests\Models\Tag;
use Tests\Models\User;

Expand Down Expand Up @@ -271,10 +271,10 @@ public function testWithPivot()
public function testWithPivotClass()
{
$permissions = User::first()->permissions()
->withPivot('role_user', ['role_role_pk'], RoleUserPivot::class, 'pivot')
->withPivot('role_user', ['role_role_pk'], RoleUser::class, 'pivot')
->get();

$this->assertInstanceOf(RoleUserPivot::class, $pivot = $permissions[0]->pivot);
$this->assertInstanceOf(RoleUser::class, $pivot = $permissions[0]->pivot);
$this->assertEquals(['role_role_pk' => 1], $pivot->getAttributes());
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Models/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function permissions()

public function permissionsWithPivotAlias()
{
return $this->hasManyDeep(Permission::class, [User::class, RoleUserPivot::class.' as alias', Role::class]);
return $this->hasManyDeep(Permission::class, [User::class, RoleUser::class.' as alias', Role::class]);
}

public function posts()
Expand Down
4 changes: 1 addition & 3 deletions tests/Models/RoleUserPivot.php → tests/Models/RoleUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
use Illuminate\Database\Eloquent\Relations\Pivot;
use Staudenmeir\EloquentHasManyDeep\HasTableAlias;

class RoleUserPivot extends Pivot
class RoleUser extends Pivot
{
use HasTableAlias;

protected $table = 'role_user';
}

1 comment on commit 48e5a70

@budhajeewa
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating the README, @staudenmeir ! :)

Ref: #26 (comment)

Please sign in to comment.