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

[5.x] Add findOrNew, firstOrCreate, createOrUpdate (& related methods) methods to Entry & Term query builders #9815

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

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.

src/Stache/Query/EntryQueryBuilder.php Outdated Show resolved Hide resolved
src/Facades/Entry.php Outdated Show resolved Hide resolved
@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

2 participants