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

[9.x] Make Connection Class Macroable #41865

Merged

Conversation

haider00125
Copy link
Contributor

I having a use case where I have to check database connection in different service providers. I am doing it in following way:

try {
    DB::connection()->getPdo();
} catch (Exception) {
    return false;
    // Don't proceed further;
}

If connection class is macroable then above check can be simplified like below.

  DB::macro('connected', function (string $connection = null) {
            try {
                DB::connection($connection)->getPdo();
                return true;
            } catch (Exception) {
                return false;
            }
  });

Implementation:

if (DB::connected()) {
     // Do something.
}

@taylorotwell taylorotwell merged commit c195fd9 into laravel:9.x Apr 7, 2022
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.

None yet

2 participants