Support the ongoing development of Laravel.io →
posted 1 month ago
Laravel
0
moderator

Hello @nazreen-cod

Can you show what you have tried? That can help people to answer your question :)

0

hi @tvbeek

I'm trying to connect a Laravel web application to Firebase, but I've run into difficulties. Specifically, I followed a tutorial and installed the necessary package (kreait/laravel-firebase), but I’m unable to establish a connection.

Steps I’ve Taken: 1.Created a new Laravel project:laravel new fyp_firebase

2.Installed the kreait/laravel-firebase package:composer require kreait/laravel-firebase

3.Downloaded the Firebase Service Account JSON key from the Firebase Console (Settings → Service Accounts → Generate New Private Key).

4.Configured the .env file to add Firebase credentials:FIREBASE_CREDENTIALS=/path/to/firebase-service-account.json

5.Published the Firebase configuration to configure the package: php artisan vendor:publish --provider="Kreait\Laravel\Firebase\ServiceProvider"

6.Configured config/firebase.php: The Firebase config file was automatically generated, and I updated it to include the correct path to the Firebase service account key:

return [
    'credentials' => env('FIREBASE_CREDENTIALS'),
    // other settings like database URL
];
Last updated by @nazreen-cod 1 month ago.

xuliangshao, phil-reck liked this reply

2
moderator

@nazreen-cod can you show the error that you got? Or what did happen? (I never used firebase, but with an error there is a higher change to find the problem :) )

0

You are right, It will make it easy to solve after seeing that.

0

Connecting Firebase with Laravel can be accomplished using Firebase's Admin SDK or third-party Laravel packages. Here’s a step-by-step guide to get you started:


Option 1: Using Firebase Admin SDK (Recommended)

1. Install Firebase Admin SDK

Install the official Firebase Admin SDK package via Composer:

composer require kreait/laravel-firebase

2. Publish the Configuration

Publish the configuration file for Firebase:

php artisan vendor:publish --provider="Kreait\Laravel\Firebase\FirebaseServiceProvider"

This will create a configuration file config/firebase.php.

3. Obtain Firebase Service Account Key

  • Go to Firebase Console.
  • Navigate to Project Settings > Service Accounts.
  • Click Generate New Private Key to download the JSON file.

4. Configure Laravel

  • Move the JSON file to your Laravel project (e.g., storage/app/firebase-service-account.json).
  • Update the FIREBASE_CREDENTIALS in your .env file:
FIREBASE_CREDENTIALS=/absolute/path/to/storage/app/firebase-service-account.json

5. Basic Usage

You can now use Firebase in your Laravel application. Here's an example to interact with Firebase Realtime Database:

use Kreait\Firebase\Factory;
use Kreait\Firebase\Contract\Database;

public function getFirebaseData()
{
    $firebase = (new Factory)
        ->withServiceAccount(config('firebase.credentials.file'))
        ->withDatabaseUri('https://your-project-id.firebaseio.com/');

    $database = $firebase->createDatabase();

    // Fetch data from a Firebase path
    $data = $database->getReference('path/to/your/data')->getValue();

    return response()->json($data);
}

You can also interact with Firebase Authentication, Cloud Messaging, and more. Check the package documentation.


Option 2: Using a Third-Party Laravel Package

Alternatively, you can use a third-party package like laravel-firebase-admin.

Installation

composer require kreait/laravel-firebase

The steps after this are similar to Option 1.


Common Use Cases

1. Firebase Authentication

Authenticate users with Firebase in Laravel:

use Kreait\Firebase\Auth;

public function authenticateUser($idToken)
{
    $auth = app('firebase.auth');

    try {
        $verifiedIdToken = $auth->verifyIdToken($idToken);
        $uid = $verifiedIdToken->claims()->get('sub');

        return response()->json(['uid' => $uid]);
    } catch (\Kreait\Firebase\Exception\Auth\InvalidIdToken $e) {
        return response()->json(['error' => 'Invalid token'], 401);
    }
}

2. Firebase Cloud Messaging (FCM)

Send push notifications using Firebase:

use Kreait\Firebase\Messaging\CloudMessage;

public function sendNotification()
{
    $messaging = app('firebase.messaging');

    $message = CloudMessage::fromArray([
        'token' => 'user-device-token',
        'notification' => ['title' => 'Hello!', 'body' => 'This is a test notification.'],
    ]);

    $messaging->send($message);
}

Debugging Tips

  1. Verify the JSON Path: Ensure the FIREBASE_CREDENTIALS path in .env is correct and points to the service account file.
  2. Database URI: Ensure you specify the correct database URL in your Firebase project.
  3. Enable Services: Check that the Firebase services you’re using (like Realtime Database, Firestore, or FCM) are enabled in the Firebase console.

If you still face issues, feel free to provide more details about the error or problem you’re encountering!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

nazreen nazreen-cod Joined 12 Dec 2024

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.