kreait / laravel-firebase

A Laravel package for the Firebase PHP Admin SDK
https://github.com/kreait/firebase-php
MIT License
994 stars 163 forks source link

Laravel 8 Target class [firebase.database] does not exist for Real Time DB. #98

Closed shrirangjoshi94 closed 2 years ago

shrirangjoshi94 commented 2 years ago

Hello, I migrated to Laravel 8.5 from Laravel 7. After migrating I am getting this error when trying to instantiate the firebase real-time database.

I am using "kreait/firebase-php": "^5.20

After doing this $database = app('firebase.database'); I get the below error

Target class [firebase.database] does not exist. {"userId":1,"exception":"[object] (Illuminate\Contracts\Container\BindingResolutionException(code: 0): Target class [firebase.database] does not exist. at /var/www/html/meds-be/vendor/laravel/framework/src/Illuminate/Container/Container.php:875)

I tried another approach to instantiate the database as mentioned in the documentation

After doing this use Kreait\Firebase\Database;

class MyService { public function __construct(Database $database) { $this->database = $database; } }

I get this error

local.ERROR: Target [Psr\Http\Message\UriInterface] is not instantiable while building Kreait\Firebase\Database]. {"exception":"[object] (Illuminate\Contracts\Container\BindingResolutionException(code: 0): Target [Psr\Http\Message\UriInterface] is not instantiable while building Kreait\Firebase\Database]. at /var/www/html/meds-be/vendor/laravel/framework/src/Illuminate/Container/Container.php:1089)

Can anyone please help me with this?

jeromegamez commented 2 years ago

The table in the readme is unfortunately outdated - could you please check if use Kreait\Firebase\Contracts\Database works?

Which version of kreait/laravel-firebase have you installed? Is it the latest version?

shrirangjoshi94 commented 2 years ago

Hi Jerome, Thank you for your response. We are using the latest version "kreait/firebase-php": "5.21.0"

We just tried the thing you suggested but it is not working. The error we get is Database is an interface that cannot be instantiated,

Could you please provide us a small example of how to implement it?

My current implementation is. I am trying to using is as a helper throughout the application. It was working earlier with Laravel 7 not sure what changed with Laravel 8.5.

<?php

declare(strict_types=1);

namespace App\Helpers;

use Exception;

class FirebaseHelper
{
    /**
     * Push the data to firebase.
     *
     * @param string $path
     * @param array $data
     * @return bool
     */
    public static function set(string $path, array $data): bool
    {
        try {
            $database = app('firebase.database');
            $database->getReference($path)->set($data);

            return true;
        } catch (Exception $exception) {
            report($exception);

            return false;
        }
    }
}

Thank you very much for your time.

Kind Regards, Shrirang

jeromegamez commented 2 years ago

I just tried it out on a local installation (Laravel 8 and PHP 8) and it worked for me 🤔, strange…

You said that you have installed "kreait/firebase-php": "5.21.0", but have you also installed "kreait/laravel-firebase": "3.1"? I'm asking just to be sure 😅.

If you're on PHP <7.4, please let me know as well, that would make things a little bit more complicated 😅

shrirangjoshi94 commented 2 years ago

Hi Jerome, This has been resolved. You are right I was not using the Laravel specific package :sweat_smile:. I was using kreait/firebase-php and not kreait/laravel-firebase. I changed the package and it started working. Yes, I am using PHP 7.4 but I haven't faced any issue with it. Thank you for your help. Will close this issue.