Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NovaResourceProvider Incorrect Resource Mapping #132

Open
ZurabWeb opened this issue Aug 9, 2024 · 0 comments
Open

NovaResourceProvider Incorrect Resource Mapping #132

ZurabWeb opened this issue Aug 9, 2024 · 0 comments

Comments

@ZurabWeb
Copy link

ZurabWeb commented Aug 9, 2024

When two resources use one model, the resource mapping only references the last one sorted alphabetically.

app/Models/UserModel.php:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class UserModel extends Model {}

app/Nova/UserOne.php:

<?php

namespace App\Nova;

use Laravel\Nova\Http\Requests\NovaRequest;

class UserOne extends Resource {

    public static $model = \App\Models\UserModel::class;

    public function fields(NovaRequest $request): array
    {
        return [];
    }
}

app/Nova/UserTwo.php:

<?php

namespace App\Nova;

use Laravel\Nova\Http\Requests\NovaRequest;

class UserTwo extends Resource {

    public static $model = \App\Models\UserModel::class;

    public function fields(NovaRequest $request): array
    {
        return [];
    }
}

For the purposes of this test only, change the visibility of \Parental\Providers\NovaResourceProvider::setNovaResources() to public:

public function setNovaResources(): void

Tinker session without NovaResourceProvider:

> Nova::resourcesIn(app_path('Nova'));
= null

> resolve(\App\Models\UserModel::class);
= App\Models\UserModel {#6907}

> Nova::resourceForModel('App\Models\UserModel')
= "App\Nova\UserOne"

Tinker session with NovaResourceProvider:

> Nova::resourcesIn(app_path('Nova'));
= null

> $provider = new \Parental\Providers\NovaResourceProvider(app())
= Parental\Providers\NovaResourceProvider {#6906}

> $provider->setNovaResources()
= null

> resolve(\App\Models\UserModel::class);
= App\Models\CancellationPolicy {#6922}

> Nova::resourceForModel('App\Models\UserModel')
= "App\Nova\UserTwo"

As you can see from the above example, when NovaResourceProvider is booted, it changes the resource mapping, resulting in unexpected behavior.

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

No branches or pull requests

1 participant