Hassan Elshazly Eida
Hassan Elshazly Eida

Reputation: 839

How to integrate database in Firebase (Cloud Firestore) with Laravel Auth user

I want to check this user is unique before create it using Firestore with Laravel Auth/RegisterController

Cloud Firestore

Upvotes: 0

Views: 1311

Answers (1)

jeromegamez
jeromegamez

Reputation: 3541

I can see from your screenshots that you are using kreait/firebase-php (which I like since I'm the maintainer 🙈).

Were you aware that the SDK also has a Laravel Package? You can find it on GitHub at github.com/kreait/laravel-firebase.

With it, you don't need to instantiate the Factory in your controller, but can use Dependency Injection or the app() helper to retrieve the component you need.

Since I believe that the package would be the best solution for your use case, I will continue as if you were using it 😅) - once configured, you could do use a transaction (as already hinted by @Renaud Tarnec) to check for uniqueness:

/** @var \Google\Cloud\Firestore\FirestoreClient $firestore */
$firestore = app('firebase.firestore')->database();

$firestore->runTransaction(function (Transaction $transaction) {
    // ...        
});

In order to make the functionality available to the validator, you should look at creating a custom rule as described on https://laravel.com/docs/validation#custom-validation-rules - this would probably enable you to use it as you envisioned it in your screenshot.

PS: Please consider embedding images directly in your questions, or, even better, include code snippets as text - this would make it easier to grasp the question in its entirety, and enable others to copy-paste parts of it.

Upvotes: 2

Related Questions