Created
September 8, 2021 19:20
-
-
Save shawnhooper/319f90234571f90fd15b33ab07df4c74 to your computer and use it in GitHub Desktop.
Using the doesntExist() method in Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This works | |
if ( 0 === $model->where('status', 'pending')->count() ) { | |
} | |
// But since I don't care about the count, just that there isn't one | |
// Laravel's exists() method is cleaner. | |
if ( ! $model->where('status', 'pending')->exists() ) { | |
} | |
// But I find the ! in the statement above easily missed. The | |
// doesntExist() method makes this statement even clearer. | |
if ( $model->where('status', 'pending')->doesntExist() ) { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment