Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

MorphTo relationship eager loading constraints #2410

Closed
tobyzerner opened this issue Nov 7, 2020 · 1 comment
Closed

MorphTo relationship eager loading constraints #2410

tobyzerner opened this issue Nov 7, 2020 · 1 comment

Comments

@tobyzerner
Copy link

tobyzerner commented Nov 7, 2020

Much like we can specify which nested relationships to eager load for each different type in a morph-to relation:

use Illuminate\Database\Eloquent\Relations\MorphTo;

$activities = ActivityFeed::query()
    ->with(['parentable' => function (MorphTo $morphTo) {
        $morphTo->morphWith([
            Event::class => ['calendar'],
            Photo::class => ['tags'],
            Post::class => ['author'],
        ]);
    }])->get();

It would be useful to be able to apply different constraints to the queries for each type as well, something like this:

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\MorphTo;

$activities = ActivityFeed::query()
    ->with(['parentable' => function (MorphTo $morphTo) {
        $morphTo->constrain([
            Event::class => function (Builder $query) {
                $query->where('foo', 'bar');
            },
            Photo::class => function (Builder $query) {
                $query->where('foo', 'bar');
            },
        ]);
    }])->get();
@tobyzerner
Copy link
Author

PR: laravel/framework#35190

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant