Skip to content

Augmentation N+1 query issue on has_many relationships #777

Description

@mefenlon

Description

When a relationship is augmented, each of the resources' relationships are also augmented. If I add them for eager loading ('with'), they are augmented without the N+1, but that seems like a lot of augmentation for just displaying some relationship data.

Is there a way to implement some shallow augmentation on the front end like runway does for api?

// config/runway.php

   'resources' => [
        \App\Models\Title::class => [
            'name' => 'Titles',
            'route' => '/titles/{{ abbreviation }}',
            'title_field' => 'sort_title',
            'template' => 'runway.title.show',
            'read_only' => true,
            'order_by' => 'sort_title',
            'with' => [
                'runwayUri',
                'cities', // BelongsToMany relationship
                //  n+1 triggers as city is augmented and loads titles relationship
                // 'cities.titles',
                // 'cities.runwayUri',
                'tags.runwayUri',
                'dateRanges',
                'dateRangeMorphs',
                'projects', // MorphToMany relationship
                //  n+1 triggers as project is augmented and loads titles relationship
                //'projects.titles'
            ]
        ],
        \App\Models\City::class => [
            'name' => 'Cities',
            'route' => '/cities/{{ name }}',
            'title_field' => 'name',
            'template' => 'runway.city.show',
        ],

        \App\Models\Project::class => [
            'read_only' => true,
        ],
    ],

This is occurring on has_many relationships of type BelongsToMany and MorphToMany (I know, not supported, but it works)

Many repeats of the query generated from AugmentedModel.php#86

Image Image
select `titles`.*, `city_title`.`city_id` as `pivot_city_id`, `city_title`.`title_id` as `pivot_title_id` from `titles` inner join `city_title` on `titles`.`id` = `city_title`.`title_id` where `city_title`.`city_id` = 12

Steps to reproduce

Clone https://github.com/mefenlon/runway-playground.git
composer install
sail up -d
sail artisan migrate:fresh --seed
http://localhost/

laravel-query-detector will fire for the augmented fields.
If you uncomment in config/runway.php, the n+1 is avoided, but the unneeded augmentation still generates extra queries.
// 'cities.titles',
// 'cities.runwayUri',

Environment

Environment
Application Name: Laravel
Laravel Version: 12.50.0
PHP Version: 8.5.2
Composer Version: 2.9.4
Environment: local
Debug Mode: ENABLED
URL: localhost:8000
Maintenance Mode: OFF
Timezone: UTC
Locale: en

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: log
Cache: database
Database: mysql
Logs: stack / single
Mail: log
Queue: database
Session: database

Storage
public/storage: NOT LINKED

Statamic
Addons: 1
Sites: 1
Stache Watcher: Enabled (auto)
Static Caching: Disabled
Version: 6.2.4 Solo

Statamic Addons
statamic-rad-pack/runway: 9.0.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions