Skip to content

Conversation

@bencarr
Copy link
Contributor

@bencarr bencarr commented Apr 11, 2025

When using scoped route model binding with a child model that uses a SQID as its route key, the child model is not properly scoped to the parent.

Route::get('/customers/{customer}/{charge}')->scopeBindings();

When applying the ->whereSqid() condition, the HasSqids trait currently re-instantiates a query from the trait's parent Model instance rather than chaining onto the provided $query parameter, which would contain and instance of the parent's scoped relationship.

// HasSqids.php
public function resolveRouteBindingQuery($query, $value, $field = null): Builder|Relation 
{
    // ...

    // Current behavior
    return $this->whereSqid(sqid: $value); // Equivalent to Charge::query()->whereSqid(sqid: $value);

    // Proposed behavior
    return $query->whereSqid(sqid: $value); // Equivalent to $customer->charges->whereSqid(sqid: $value);
}

@bensherred
Copy link
Member

Thanks @bencarr, this is great

@bensherred bensherred merged commit 259707c into red-explosion:main Apr 11, 2025
11 checks passed
@bencarr bencarr deleted the fix-scoped-bindings branch April 11, 2025 15:02
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

Successfully merging this pull request may close these issues.

2 participants