【问题标题】:Connecting To Firestore Using Laravel 6 Not Working使用 Laravel 6 连接到 Firestore 不工作
【发布时间】:2019-09-20 16:35:17
【问题描述】:

我一直在尝试使用我的 laravel 6 应用程序将数据插入到 Firestore,它看起来像是在尝试连接到 mysql。

我已尝试删除 .env 文件中的 db 部分:

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

但它似乎不起作用。 首先这是我尝试插入时遇到的错误:

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

这是我的 php 代码:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Google\Cloud\Firestore\FirestoreClient;
use Google\Cloud\Firestore\FireStoreDocument;

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' => ''  
        ]);
    } 
}

我不知道它为什么要尝试使用 mysql 进行连接。任何人都知道我需要做什么或做错了什么???。

我们非常感谢每一种形式的帮助。干杯!!!!

【问题讨论】:

    标签: php laravel firebase-realtime-database google-cloud-firestore laravel-6


    【解决方案1】:

    env 文件仅用于覆盖 config/databases.php 文件中定义的默认数据库,如果您不想使用 mysql,则需要在该文件中修改此行:

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

    但是,由于它正在尝试连接到 mysql 数据库,因此在您的项目中某处您正在使用 Laravel 的 orm(雄辩),并且需要将 Firestore 更深层次地集成到 Laravel 中,或者避免使用 Eloquent。

    【讨论】:

      猜你喜欢
      • 2020-04-19
      • 1970-01-01
      • 1970-01-01
      • 2022-12-19
      • 1970-01-01
      • 2021-04-24
      • 2016-11-10
      • 2021-08-17
      • 2016-06-06
      相关资源
      最近更新 更多