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

[11.x] Add the ability to defer route model binding #54053

Closed

Conversation

dshafik
Copy link
Contributor

@dshafik dshafik commented Jan 1, 2025

I ran into the behavior that was reported in #44177 and am proposing this addition to help resolve it by deferring the query until the model is used.

To be clear: this changes no existing behavior. There is a single thing I couldn't resolve with this, which is that when using scoped bindings only the final child model will end up being deferred. I don't think this is a big deal, but it would be great if the entire chain could be deferred — the problem is there's no way to get the relationship to figure out the child model without it pulling the id of the parent and that implicitly resolves the parent.

This probably should just be a package, but I wanted to see if there was interest in including it in the core framework first.

Here's a draft attempt at docs to explain it:

Deferred Binding

By default, models are resolved before route middleware has been executed, this means that any global scopes applied to the model that rely on data created in middleware will not function as expected.

To resolve this, you can use the DeferRouteBinding trait. When used in your models, the query is deferred when binding until either explicitly or implicitly loaded within the method itself:

class MyModel extends Model {
    use DeferResolveRouteBinding;

    …
}

class MyController {
    /**
     * @throws ModelNotFound when attempting to load the model if not found
     */
    public function update(Request $request, MyModel $myModel) {
      // loads implicitly most of the time when using the model:
      $myModel->update($request->validated());
      
      // OR load explicitly and then use it as normal:
      $myModel();
    }
  }

You should ensure to load the model prior to executing any other code that would later fail if the model is not found.

Note: when using scoped bindings, only the final "child" model will be deferred.

@dshafik dshafik changed the title Feature: Lazily resolve route model binding [11.x] Add the ability to lazily resolve route model binding Jan 1, 2025
@dshafik dshafik force-pushed the lazily-resolve-route-model-binding branch 2 times, most recently from cd86130 to 8a7b787 Compare January 1, 2025 12:24
@dshafik dshafik changed the title [11.x] Add the ability to lazily resolve route model binding [11.x] Add the ability to defer route model binding Jan 1, 2025
@dshafik dshafik force-pushed the lazily-resolve-route-model-binding branch 3 times, most recently from 8c198e8 to 919ae88 Compare January 2, 2025 03:09
@dshafik dshafik force-pushed the lazily-resolve-route-model-binding branch from 919ae88 to 61e6686 Compare January 2, 2025 04:01
@taylorotwell
Copy link
Member

If this can be handled in user land I would much rather it be handled there via a package. 🫶

@dshafik
Copy link
Contributor Author

dshafik commented Jan 3, 2025

If this can be handled in user land I would much rather it be handled there via a package. 🫶

Insofar as this works, yes. If you wanted to solve the scoped bindings are all deferred, then no, it requires a change in how relationships are created. I didn't want to do that work without feedback.

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