Skip to content

Conversation

@godismyjudge95
Copy link
Contributor

This PR adds a few helper methods to the repositories/facades: firstOrCreate and updateOrCreate.
These methods exist on Laravel eloquent models:
https://laravel.com/docs/11.x/eloquent#retrieving-or-creating-models
https://laravel.com/docs/11.x/eloquent#updates

As a result, this PR attempts to replicate that functionality but for Statamic's content types.

I am marking this as a draft as I would like to implement this for all reasonable facades/repositories.
If I am off track on the implementation feel free to point it out.

@godismyjudge95
Copy link
Contributor Author

I removed the createOrFirst method as Statamic does not throw an exception when the entry/term already exists. This means the method would always create/update and never just return the entry/term.

@godismyjudge95
Copy link
Contributor Author

@duncanmcclean unless you had any other comments on major portions of this, I think it is ready for review?

@duncanmcclean duncanmcclean marked this pull request as ready for review April 5, 2024 20:06
@duncanmcclean
Copy link
Member

Thanks! We'll review this when we can.

@duncanmcclean duncanmcclean changed the title [5.x] Add firstOrCreate and updateOrCreate methods to repositories/facades [5.x] Add findOrNew, firstOrCreate, createOrUpdate (& related methods) methods to Entry & Term query builders Apr 15, 2024
Copy link
Member

@duncanmcclean duncanmcclean left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your work on this pull request!

I've made a couple of tweaks to the tests and implemented a few of the other methods missing from our query builder implementation.

I've also opened #9976 which includes some of the methods useful for all of Statamic's query builders.

@godismyjudge95
Copy link
Contributor Author

Thanks for your work on this pull request!

I've made a couple of tweaks to the tests and implemented a few of the other methods missing from our query builder implementation.

I've also opened #9976 which includes some of the methods useful for all of Statamic's query builders.

Wow thank you for all of these improvements! This will help a ton in our use cases.

@duncanmcclean duncanmcclean changed the base branch from master to 5.x July 18, 2024 14:15
@johncarter-
Copy link
Contributor

Thanks @godismyjudge95 for dedicating your time to this, such a great PR.

I'd love to see this merged 🙏🏻

@godismyjudge95
Copy link
Contributor Author

@duncanmcclean I know this was approved awhile ago, are there any more blockers or things I can do to help the process? If not that is ok, just wanted to check and make sure.

@duncanmcclean
Copy link
Member

@duncanmcclean I know this was approved awhile ago, are there any more blockers or things I can do to help the process? If not that is ok, just wanted to check and make sure.

Nah, it's just waiting on a final review on our side. 👍

@jasonvarga jasonvarga changed the base branch from 5.x to master November 11, 2025 20:17
@jasonvarga jasonvarga changed the title [5.x] Add findOrNew, firstOrCreate, createOrUpdate (& related methods) methods to Entry & Term query builders [6.x] Add findOrNew, firstOrCreate, createOrUpdate (& related methods) methods to Entry & Term query builders Nov 11, 2025
@jasonvarga jasonvarga changed the title [6.x] Add findOrNew, firstOrCreate, createOrUpdate (& related methods) methods to Entry & Term query builders [6.x] Add Entry and Term methods to Repos and Query Builders Nov 11, 2025
@jasonvarga jasonvarga changed the title [6.x] Add Entry and Term methods to Repos and Query Builders [6.x] Add methods to Entry/Term Repos and Query Builders Nov 11, 2025
@jasonvarga jasonvarga changed the title [6.x] Add methods to Entry/Term Repos and Query Builders [6.x] Add methods to Entry/Term Repos Nov 12, 2025
@jasonvarga
Copy link
Member

jasonvarga commented Nov 12, 2025

Sorry to be a buzzkill, but I've removed the query builder methods. I didn't really want to introduce the concept of updating/creating things by "attributes" as they don't really translate well to Statamic objects too often. We don't have a "create" concept either (e.g. Entry::create()). There's often extra steps after newing up the entry/term. There's the extra layer of awkwardness with taxonomies in that you might get a localizedterm.

As much as I would love for our query builders to be 1:1 with Eloquent, they are really more just "heavily inspired".

I know that means this PR isn't really much anymore. Maybe the meat of it can return at a later date.

Thanks for understanding.

@jasonvarga jasonvarga merged commit 3f47310 into statamic:master Nov 12, 2025
19 checks passed
@godismyjudge95 godismyjudge95 deleted the update-or-create branch November 12, 2025 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects
Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants