Skip to content

Eager load polymorphic relationships with constrain() #6507

Discussion options

You must be logged in to vote

I can't believe I completely forgot the existence of the indexQuery() method.
I used the following for eager loading:

public static function indexQuery(NovaRequest $request, $query): Builder
{
    return parent::indexQuery($request, $query)
        ->with([
            'model' => function ($morphTo) {
	            $morphTo->constrain([
	                \App\Models\Foo::class => function ($query) {
	                    $query->with(['relation']);
	                },
	                \App\Models\Bar::class => function ($query) {
	                    $query->with(['different_relation']);
	                },
	                \App\Models\Baz::class => function ($query) {
	                    /…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by kiritokatklian
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant