Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

developer alamin's avatar

Call to a member function connection() on null in laravel

Call to a member function connection() on null when i run php artisan optimize:clear command in laravel.

0 likes
28 replies
LaryAI's avatar
Level 58

This error occurs when Laravel is unable to establish a database connection.

To resolve this issue, check your database configuration in the .env file and ensure that the database credentials are correct. Also, make sure that the database server is running.

If the issue persists, try clearing the configuration cache by running the following command:

php artisan config:clear

If that doesn't work, try clearing the application cache by running:

php artisan cache:clear

If none of these solutions work, try restarting your web server and database server.

If the issue still persists, it may be helpful to provide more information about your Laravel and database setup, as well as any relevant error messages.

developer alamin's avatar

@Snapey When I run php artisan serve . Show this error on my terminal/.

In Model.php line 1820: Call to a member function connection() on null

Snapey's avatar

@developer alamin for reference, these are your database settings;

DB_CONNECTION=mysql 
DB_HOST=127.0.0.1
DB_PORT=3306 
DB_DATABASE=ai_article 
DB_USERNAME=admin 
DB_PASSWORD=admin

open tinker (if you can) and run

> config('database.default')

and then

> config('database.connections.mysql')

and check it has your database credentials

developer alamin's avatar

@Snapey i kno tinker . NO artisan command run When I give artisan command such as php artisan tinker . In those time show this error .

JussiMannisto's avatar

@developer alamin Delete this message. Your .env contains sensitive information such as login credentials and api keys. You should then change the passwords and regenerate the keys you just shared.

Cc: @JeffreyWay

AddWebContribution's avatar

@developer alamin

To resolve this error, you can follow the following steps:

Check your database credentials in the .env file of your Laravel application.

Ensure that your database server is up and running.

Clear the application cache by running the following command:

php artisan cache:clear

Clear the application configuration cache by running the following command:

php artisan config:cache

If you are still experiencing the error, try restarting your web server.

Make sure that your database is configured properly and the tables are created if not then run migration to create tables by running the following command:

php artisan migrate

If none of the above solutions work, then check if you have any syntax error in your code, or try to check the error logs for more information.

developer alamin's avatar

@saurabhd When i run any command show this error . so you give me some steps for following but when run command so this error

tykus's avatar

Do you have a service provider that is trying to access Model(s) from the database before the config is ready?

tykus's avatar

@developer alamin there are ServiceProviders which register services into the Container. Inside each ServiceProvider class, there (usually) is a boot and a register method; you cannot access the database inside the register method because the config is not set yet.

Look at the classes inside the app/Providers directory

developer alamin's avatar

@tykus

/**
 * Register any application services.
 *
 * @return void
 */
public function register()
{
    //
}

/**
 * Register a booting callback to be run before the "boot" method is called.
 *
 * @param  \Closure  $callback
 * @return void
 */
public function booting(Closure $callback)
{
    $this->bootingCallbacks[] = $callback;
}

/**
 * Register a booted callback to be run after the "boot" method is called.
 *
 * @param  \Closure  $callback
 * @return void
 */
public function booted(Closure $callback)
{
    $this->bootedCallbacks[] = $callback;
}
tykus's avatar

@developer alamin not interested in the parent ServiceProvider class; interested in your Service Providers! Specifically interested in any Service Provider class where you are accessing the database!

developer alamin's avatar

@tykus Can i access in existing provider file. How to access share DB its explain plz..plz . This error gives me pain.

tykus's avatar

@developer alamin I can't help you without knowing what your code might look like ¯\_(ツ)_/¯

developer alamin's avatar

@tykus actually how to use database connection in register() of any service provider. It should always go in boot(). I f you give example i can't any example in it

tykus's avatar

@developer alamin are you using the database / Models in one of your service Providers???

kokoshneta's avatar

Since your English is very basic, here are some simple instructions to try. Please write a comment here and answer all these questions:

  1. Do you have a MySQL database running?
  2. Is it running on 127.0.0.1, on port 3306?
  3. Is your database called ai_article?
  4. Is the username and password for your database admin?

Then do this:

  • Open the folder app/Providers
  • Open every file in the folder
  • Copy the contents of the boot() function from each file
  • Paste it into your answer
developer alamin's avatar

@kokoshneta I know that my english is basic but thats enough for me okay . You shouldn't personal attack anyone. If you know to answer in my question you should to discuss about it. I hope you don't do this again in the future anybody. No one knowing everything by born . Keep it mind.

Snapey's avatar

@developer alamin @kokoshneta is just trying to make it easy for you to answer the questions we have about your situation so that we can propose a solution. You are leaving out vital information, and despite clear instructions, you still choose to ignore requests for further information.

@kokoshneta in no way insulted you in his response, but I guess you have destroyed any chance of getting this question answered now.

Bye.

2 likes
kokoshneta's avatar

@developer As @snapey said, the purpose of my post was to avoid using complex words and constructions that you might not understand. I wrote in simpler, shorter sentences in order to make sure that you understood everything. Others had previously asked you for more information, and your answers to them had been largely irrelevant to the questions they asked, so it was not clear if you had understood the words they used, or if perhaps you are not familiar with the Laravel concepts they were asking about (such as service providers).

The fact that your English is basic is not a personal attack, it just means that you have not had much exposure to English. My Japanese is very basic, and there’s nothing wrong with that.

But you still didn’t answer any of the questions.

newbie360's avatar

@developer alamin OMG, You better remove all the sensitive informations from your post

when you use Gmail smtp, avoid use the gmail master password for access the google service.

instead, you should in Google account create an app-token via the setting > security, so other peoples can't use the app-token to login your gmail account

Please or to participate in this conversation.