Skip to content

[Performance] Fix IdeaPolicy N+1 queries on ownership checks #50

@ibourgeois

Description

@ibourgeois

Problem

Every ownership check in IdeaPolicy calls $idea->user->is($user), which fires a DB query to load the user relation. With 5 policy methods (view, update, delete, restore, forceDelete), this means up to 5 unnecessary queries per authorization check.

Required Change

Replace every $idea->user->is($user) with $idea->user_id === $user->id in all 5 methods.

Files

  • app/Policies/IdeaPolicy.php

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    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