Skip to content

Commit

Permalink
Support related models with custom table
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Aug 21, 2021
1 parent 8ec2806 commit c441156
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/ConcatenatesRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ protected function hasOneOrManyDeepFromRelations(array $relations)

if ($i === count($relations) - 1) {
$related = get_class($relation->getRelated());

if ((new $related())->getTable() !== $relation->getRelated()->getTable()) {
$related .= ' from ' . $relation->getRelated()->getTable();
}
} else {
$through[] = $this->hasOneOrManyThroughParent($relation, $relations[$i + 1]);
}
Expand Down
8 changes: 7 additions & 1 deletion src/HasRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ public function hasOneDeepFromRelations(...$relations)
*/
protected function hasOneOrManyDeep($related, array $through, array $foreignKeys, array $localKeys)
{
$relatedSegments = preg_split('/\s+from\s+/i', $related);

/** @var \Illuminate\Database\Eloquent\Model $relatedInstance */
$relatedInstance = $this->newRelatedInstance($related);
$relatedInstance = $this->newRelatedInstance($relatedSegments[0]);

if (isset($relatedSegments[1])) {
$relatedInstance->setTable($relatedSegments[1]);
}

$throughParents = $this->hasOneOrManyDeepThroughParents($through);

Expand Down
10 changes: 10 additions & 0 deletions tests/HasManyDeepTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests;

use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
use Tests\Models\Comment;
Expand Down Expand Up @@ -345,4 +346,13 @@ public function testFromRelationsWithAlias()

$this->assertEquals([35], $comments->pluck('id')->all());
}

public function testFromRelationsWithCustomTable()
{
DB::schema()->rename('comments', 'my_comments');

$comments = Country::first()->commentsFromRelationsWithCustomTable;

$this->assertEquals([31, 32], $comments->pluck('id')->all());
}
}
14 changes: 14 additions & 0 deletions tests/Models/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ public function commentsFromRelations()
return $this->hasManyDeepFromRelations([$this->posts(), (new Post())->comments()]);
}

public function commentsFromRelationsWithCustomTable()
{
$comment = (new Comment())->setTable('my_comments');

$comments = (new Post())->newHasMany(
$comment->newQuery(),
$this,
$comment->getTable().'.post_id',
'id'
);

return $this->hasManyDeepFromRelations([$this->posts(), $comments]);
}

public function commentsWithTrashedUsers()
{
return $this->comments()->withTrashed('users.deleted_at');
Expand Down

0 comments on commit c441156

Please sign in to comment.