Skip to content

[7.x] Allow to use alias of morphed model - #34032

Merged
taylorotwell merged 2 commits into
laravel:7.xfrom
thewebartisan7:patch-1
Aug 27, 2020
Merged

taylorotwell merged 2 commits into
laravel:7.xfrom
thewebartisan7:patch-1

Conversation

@thewebartisan7

@thewebartisan7 thewebartisan7 commented Aug 27, 2020 •

Copy link
Copy Markdown
Contributor

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');

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');
```
@driesvints driesvints changed the title Allow to use alias of morphed model [7.x] Allow to use alias of morphed model Aug 27, 2020
@taylorotwell
taylorotwell merged commit 6a49976 into laravel:7.x Aug 27, 2020
salmanarshad321 pushed a commit to salmanarshad321/framework that referenced this pull request Sep 23, 2026
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants