LoginSignup
3
0

More than 1 year has passed since last update.

phpでfirestoreに接続

Last updated at Posted at 2021-06-24

laravel環境用意

  • Laravel Sail

   私はphp8の方使用。

   groupadd: invalid group ID 'sail' 問題にぶつかる以下解決

grpcインストール

  • DockerFileにgrpc、protobuf(grpcのパフォーマンスを向上させるライブラリ)をインストールするために記述追加
    pecl install grpc && \
    pecl install protobuf && \
  • php.iniに追加

    extension=grpc.so
    extension=protobuf.so
    
  • docker build

  • コンテナーに入ってcomposer require

    composer require google/cloud-firestore
    composer require kreait/laravel-firebase
    

    composer require kreait/laravel-firebaseはいらないかもなあ

  • .envファイルに追加 hogehoge.jsonは以下設定で取得したファイル

    GOOGLE_APPLICATION_CREDENTIALS="/var/www/html/hogehoge.json"
    

設定

Cloud Firestore を使ってみる | Firebase

  • Google アプリケーションのデフォルト認証情報

    • サービスアカウント作成画面に移動

      Google Cloud Platform

    • プロジェクトを選択

    • サービス アカウントの作成

      サービスカウント名、説明(必要なら)を入力、作成して実行押下。

    • ロールを選択
      cloud databastoreユーザ選択、続行、許可

    • サービス アカウント一覧画面で先程作成したメールアドレスを押下

    • キーを押下、鍵を追加、新しい鍵を作成、jsonが選択されている状態で作成押下

    • ここで作られたjsonファイルがGOOGLE_APPLICATION_CREDENTIALS

  • Laravelでとりあえずデータを取得してみる

    Hogecontroller.php

    <?php
    
    namespace App\Http\Controllers;
    
    use Google\Cloud\Firestore\FirestoreClient;
    
    class HogeController extends Controller
    {
        public function get() {
            // Create the Cloud Firestore client
            $db = new FirestoreClient();
            printf('Created Cloud Firestore client with default project ID.' . PHP_EOL);
            $usersRef = $db->collection('users');
            $snapshot = $usersRef->documents();
    
            return view('document', [
                'snapshot' => $snapshot,
            ]);
        }
    }
    

ブラウザでデータ取得できていること確認

参照

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0