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 firstOrfail, firstOr, sole and exists methods to base query builder #9976

Open
wants to merge 9 commits into
base: 5.x
Choose a base branch
from

Conversation

duncanmcclean
Copy link
Member

This pull request adds various useful methods to Statamic's base query builder:

  • firstOrFail
    • Gets the first result if one exists, otherwise, it throws an exception.
  • firstOr
    • Gets the first result if one exists, otherwise, it executes the provided callback.
  • sole
    • Gets the first & only item if one exists. It'll throw an exception if no results can be found or if more than 1 result is found.
  • exists
    • Returns true if the query returns one or more results.

On the back of #9815, I was thinking a few of Laravel's first/find methods could be added to our "base" query builder since they're useful for everything, not just entries & terms.

…-base-query-builder

# Conflicts:
#	tests/Data/Entries/EntryQueryBuilderTest.php
@duncanmcclean duncanmcclean changed the base branch from 4.x to 5.x May 13, 2024 11:48
@duncanmcclean duncanmcclean changed the title [4.x] Add firstOrfail, firstOr, sole and exists methods to base query builder [5.x] Add firstOrfail, firstOr, sole and exists methods to base query builder May 13, 2024
…-base-query-builder

# Conflicts:
#	tests/Data/Entries/EntryQueryBuilderTest.php
@godismyjudge95
Copy link
Contributor

I could use this on the UserQueryBuilder too :)

@duncanmcclean
Copy link
Member Author

I could use this on the UserQueryBuilder too :)

This PR adds the method's to Statamic's base Builder class which gets extended by the other query builders, so they should work for users too.

@godismyjudge95
Copy link
Contributor

I could use this on the UserQueryBuilder too :)

This PR adds the method's to Statamic's base Builder class which gets extended by the other query builders, so they should work for users too.

Oh, I tried to use it in a project with composer patches, and it said method not found. Might be my issue though.

@duncanmcclean
Copy link
Member Author

Are you using flat-file or Eloquent users?

@godismyjudge95
Copy link
Contributor

Are you using flat-file or Eloquent users?

Flat file

@duncanmcclean
Copy link
Member Author

duncanmcclean commented Aug 6, 2024

Hmm, this seems to work for me (I'm also using flat-file users):

use Statamic\Facades\User;

User::query()->firstOrFail('ca291c8b-5629-4891-b506-bb2d7d9ad447');

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