Skip to content

Instantly share code, notes, and snippets.

@shawnhooper
Created September 8, 2021 19:20
Show Gist options
  • Save shawnhooper/319f90234571f90fd15b33ab07df4c74 to your computer and use it in GitHub Desktop.
Save shawnhooper/319f90234571f90fd15b33ab07df4c74 to your computer and use it in GitHub Desktop.
Using the doesntExist() method in Laravel
<?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