Skip to content

Commit

Permalink
[7.x] Allow to use alias of morphed model (#34032)
Browse files Browse the repository at this point in the history
* Allow to use alias of morphed model

At the moment if you use hasMorph() you need to use model class name and can't use alias, you need to use `Relation::getMorphedModel`.
But this is already done when you use `*` which auto discover morphed relations.

Moving this conversion for all case allow to use directly aliases.

Example now:


```
        $morphable =  [
                "App\AnotherModel",
                Relation::getMorphedModel('alias')
        ];
        $dataset = MyModel::query()
            ->whereHasMorph('morphable', $morphable)
            ->with('morphable')
            ->orderBy('id')
            ->limit(50)
            ->get()
            ->groupBy('morphable_type');
```

With this change can be:

```
        $morphable =  [
                "App\AnotherModel",
                'alias',
                'another',
                "App\YetAnotherModel",
        ];
        $dataset = MyModel::query()
            ->whereHasMorph('morphable', $morphable)
            ->with('morphable')
            ->orderBy('id')
            ->limit(50)
            ->get()
            ->groupBy('morphable_type');
```

* Update QueriesRelationships.php
  • Loading branch information
thewebartisan7 committed Aug 27, 2020
1 parent fad3e5f commit 6a49976
Showing 1 changed file with 3 additions and 3 deletions.
Expand Up @@ -205,10 +205,10 @@ public function hasMorph($relation, $types, $operator = '>=', $count = 1, $boole

if ($types === ['*']) {
$types = $this->model->newModelQuery()->distinct()->pluck($relation->getMorphType())->filter()->all();
}

foreach ($types as &$type) {
$type = Relation::getMorphedModel($type) ?? $type;
}
foreach ($types as &$type) {
$type = Relation::getMorphedModel($type) ?? $type;
}

return $this->where(function ($query) use ($relation, $callback, $operator, $count, $types) {
Expand Down

0 comments on commit 6a49976

Please sign in to comment.