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

Can see Create button on relation, but 404 when actually creating #6050

Closed
bkuhl opened this issue Nov 19, 2023 · 3 comments
Closed

Can see Create button on relation, but 404 when actually creating #6050

bkuhl opened this issue Nov 19, 2023 · 3 comments
Labels
needs more info More information is required

Comments

@bkuhl
Copy link

bkuhl commented Nov 19, 2023

  • Laravel Version: 10.32.1
  • Nova Version: 4.31.3
  • PHP Version: 8.2
  • Database Driver & Version:

Description:

I can see the Create button, but clicking it shows a 404.

Detailed steps to reproduce the issue on a fresh Nova installation:

Screenshot 2023-11-18 at 8 41 56 PM

when I click Create it sends a GET request to /nova-api/affiliates/field/campaigns?resourceName=affiliate-campaigns&viaResource=affiliates&viaResourceId=1&viaRelationship=campaigns which results in a 404.

Relevant policies:

class AffiliatePolicy
{
    use HandlesAuthorization;

    public function viewAny(User $user): bool
    {
        return true;
    }

    public function view(User $user): bool
    {
        return true;
    }

    public function create(User $user): bool
    {
        return $user->can(Ability::MANAGE_AFFILIATES);
    }

    public function update(User $user): bool
    {
        return $user->can(Ability::MANAGE_AFFILIATES);
    }

    public function replicate(User $user): bool
    {
        return false;
    }
}

class AffiliateCampaignPolicy
{
    use HandlesAuthorization;

    public function viewAny(User $user): bool
    {
        return true;
        return $user->can(Ability::MANAGE_AFFILIATES);
    }

    public function view(User $user): bool
    {
        return true;
        return $user->can(Ability::MANAGE_AFFILIATES);
    }

    public function create(User $user): bool
    {
        return true;
        return $user->can(Ability::MANAGE_AFFILIATES);
    }

    public function update(User $user): bool
    {
        return true;
        //return $user->can(Ability::MANAGE_AFFILIATES);
    }
}

Relevant models:

class Affiliate extends Model
{
    use HasFactory;

    protected $guarded = [
        'id',
    ];

    public function campaigns(): HasMany
    {
        return $this->hasMany(AffiliateCampaign::class);
    }
}

class AffiliateCampaign extends Model
{
    use HasFactory;

    protected $guarded = [
        'id',
    ];

    public function affiliate(): BelongsTo
    {
        return $this->belongsTo(Affiliate::class);
    }
}

What could cause this 404?

@bkuhl
Copy link
Author

bkuhl commented Nov 19, 2023

This is more of a Laravel thing I suppose, but I'd love it if there was a way to add a flag that would enable DEBUG level logging for policies, listing each method that's checked to help me figure out what needs to be modified for permissions.

@crynobone
Copy link
Member

Unable to reproduce the issue, please provide full reproducing repository based on fresh installation as suggested in the bug report template (or you can refer to https://github.com/nova-issues for example)

@crynobone crynobone added the needs more info More information is required label Nov 20, 2023
@bkuhl
Copy link
Author

bkuhl commented Nov 21, 2023

I've been able to narrow down why this is happening. It looks like the child resource must be defined on the parent fields() resource in order for Nova to manage the relations, otherwise it shows a 404 because it sees the field as not being there. Here's the detials:

My Affiliate resource contains:


    public function fields(NovaRequest $request)
    {
        return [
            Text::make('Name'),

            Number::make('Campaigns', 'campaigns_count')
                ->textAlign(Text::CENTER_ALIGN)
                ->sortable(),
                
                // Notice no relation here, only on fieldsForDetail()
        ];
    }

    public function fieldsForCreate(NovaRequest $request)
    {
        return [
            Text::make('Name'),

            BelongsTo::make('User')
                ->nullable()
                ->withoutTrashed()
                ->help('User who represents this affiliate'),
        ];
    }

    public function fieldsForUpdate(NovaRequest $request)
    {
        return [
            Text::make('Name'),
        ];
    }

    public function fieldsForDetail(NovaRequest $request)
    {
        return [
            Text::make('Name'),

            HasMany::make('Campaigns', 'affiliateCampaigns', AffiliateCampaign::class),
        ];
    }

Adding this debug code to the FieldController::__invoke() I can see it's only seeing these fields:

        foreach ($fields as $field) {
            echo $field->name.' ('.$field->attribute.')'.PHP_EOL;
        }

Output:

Name (name)
Campaigns (campaigns_count)

This means it's only pulling fields from the details(). Adding the following to fields() I'm able to attach the relation as expected:

            HasMany::make('Campaigns', 'affiliateCampaigns', AffiliateCampaign::class)
                ->onlyOnDetail(),

@bkuhl bkuhl closed this as completed Nov 21, 2023
@bkuhl bkuhl changed the title Can see Create button on relatioon, but 404 when actually creating Can see Create button on relation, but 404 when actually creating Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs more info More information is required
Projects
None yet
Development

No branches or pull requests

2 participants