Skip to content

Add support for ModelQueryBuilder subclasses #16

@JasonTheAdams

Description

@JasonTheAdams

A really common thing to do in models is add methods for querying said models based on various criteria and the like. This is great! The problem is that the model can quickly get filled up with query code, which really isn't where said code is meant to live. The model is a business-level entity, with things such as queries belonging more to the persistence bridges such as the Repository. But the Repository doesn't provide a flexible way of modifying the query itself in a declarative way (e.g. $donation->query()- $donation->subscription()->withStatus('active')->get()`).

I've also noticed some folks make Repository::prepareQuery() method, which then gets used regularly within the model. I think this can just live inside of the distinct ModelQueryBuilder sub-class, cleaning up the repository a bit. But really either way is fine.

A nice, simple way to do this is to add a newQueryBuilder method for the Model. By default this returns a new instance of ModelQueryBuilder. The Model subclass can then override this method and return it's own ModelQueryBuilder subclass.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions