Skip to content
Advertisement

Connecting To Firestore Using Laravel 6 Not Working

I’ve been trying to insert data into firestore using my laravel 6 application and it looks like its trying to connect to mysql.

I’ve tried to remove the db part in my .env file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

But it doesnt seem to work. First off this is the error am getting when i try to insert:

"message": "SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO)

This is my php code:

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use GoogleCloudFirestoreFirestoreClient;
use GoogleCloudFirestoreFireStoreDocument;

class LogisticsController extends Controller
{

    public $firestoreClient;

    /**
     * __construct initialize firestore client
     *
     * @return void
     */
    public function __construct()
    {

        $this->firestoreClient = new FirestoreClient([
            'projectId' => 'my-project-id',
        ]);

    }

    /**
     * create  a new logistics company
     *
     * @return 
     */
    protected function create(Request $request)
    {

        //get and validate form data
        $validatedData = $request->validate([
            'company_name' => 'required|unique:company_name|max:255',
            'registration_no' => 'required'
        ]);

        $company_name = $request->company_name;
        $registration_no = $request->registration_no;

        $this->firestoreClient->addDocument('Logistics_Companies', [
            'company_name' =>  $company_name,
            'registration_no' => $registration_no,
            'created_at' => new FirestoreTimestamp,
            'updated_at' => '',
            'deleted_at' => ''  
        ]);
    } 
}

I dont know why its trying to connect using mysql. Anyone know what i need to do or what am doing wrong ???.

Every form of help is highly appreciated. Cheers!!!!

Advertisement

Answer

The env file is only used to override the default database defined in the config/databases.php file, within that file you’ll need to modify this line if you don’t want to use mysql:

'default' => env('DB_CONNECTION', 'mysql')

However, since it’s trying to connect to a mysql database, somewhere in your project you’re using Laravel’s orm (eloquent) and will need to integrate Firestore at a deeper level into Laravel or refrain from using Eloquent.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement