AppDividend
Latest Code Tutorials

Laravel Firebase Tutorial

Connect Laravel With Firebase

3,533

Get real time updates directly on you device, subscribe now.

Laravel Firebase Tutorial is the topic, we will discuss today. Firebase was established in 2012 as a real-time architecture for synchronizing data between apps. It was taken by Google in 2014. It was a part of a real-time chat integration API. In this example, I will demonstrate how to connect Firebase Real-time Database with Laravel 5.6. We will add the data to the Firebase service. I will write the complete Laravel Firebase CRUD Tutorial post in the future.

Prerequisites:

  • Laravel 5.6
  • PHP 7.1
  • Google Firebase Real-time Database
  • Web server (Apache, NGINX or integrated PHP web server for testing).

Laravel Firebase Tutorial

First, we install new Laravel Project.

Step 1: Install Laravel Project

composer create-project --prefer-dist laravel/laravel laravelfirebase

Step 2: Install Dependencies

Open your terminal and enter the following command.

composer require kreait/firebase-php ^4.0

Step 3: Setup Firebase

Let’s begin with signing up for a Gmail account. First, Open Google Firebase Console by click on this link Google Firebase Console.Looks like below image.

Laravel Firebase Example

Next window, build a database project and provide information including project name. When finished, click the Create Project button.

Firebase Create Project

Wait for the project to be created and then continue. You will be automatically switched to the Firebase Dashboard.

Firebase Login

After successfully building the project, move to the Database tab, tick Rules to update read and write rules to true and then press the Publish button.

Firebase Database Read Write Access

 Step 4: Generate API Key

How to generate Firebase API key

Firebase makes an  API key for your project.Go to Project settings >>  SERVICE ACCOUNTS.

Laravel 5.6

You can download and paste into app >>  Http >> Controllers.

Step 5: Create Controller

php artisan make:controller FirebaseController --resource

It will create one controller file called FirebaseController.php.

Add the following code to the FirebaseController.php file.

//FirebaseController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Kreait\Firebase;
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;

class FirebaseController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/laravelfirebase-9d875-firebase-adminsdk-wltre-a1b8486a6c.json');
        $firebase = (new Factory)
        ->withServiceAccount($serviceAccount)
        ->withDatabaseUri('https://laravelfirebase-9d875.firebaseio.com/')
        ->create();

        $database = $firebase->getDatabase();

        $newPost = $database
        ->getReference('blog/posts')
        ->push([
        'title' => 'Laravel FireBase Tutorial' ,
        'category' => 'Laravel'
        ]);
        echo '<pre>';
        print_r($newPost->getvalue());
    }

}

Step 6: Add Route

Next step, We need to define the route in routes  >>  web.php file. So let us do it.

Route::get('firebase','FirebaseController@index');

Start Development Server by typing following command in terminal

php artisan serve

Go to this URL: http://localhost:8000/firebase

Array of data

Laravel 5.6 Firebase Example Tutorial

 Finally, Our Laravel Firebase Tutorial is over. Thanks for taking.

1 Comment
  1. Nsagha Kingsly says

    please can u give the structure of your config file

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.