Skip to content

[5.x] Add firstOrfail, firstOr, sole and exists methods to base query builder #9976

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

Merged
merged 14 commits into from
Apr 28, 2025

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');

@CedsTrash
Copy link

Hi, do we know if this PR is planned to be accepted and merged soon?

@duncanmcclean
Copy link
Member Author

Sorry, we don't have an ETA for reviewing/merging this pull request. We'll get to it when we can.

In the meantime, you can pull this PR into your project with a composer patch.

@CedsTrash
Copy link

Sorry, we don't have an ETA for reviewing/merging this pull request. We'll get to it when we can.

In the meantime, you can pull this PR into your project with a composer patch.

No worries, thanks for your answer.

@jasonvarga jasonvarga merged commit 882294e into 5.x Apr 28, 2025
25 checks passed
@jasonvarga jasonvarga deleted the add-various-methods-to-base-query-builder branch April 28, 2025 13:59
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.

4 participants