To perform CRUD (Create, Read, Update, Delete) operations in Laravel with Firebase Realtime Database, you can use the official Firebase PHP SDK. Here is an example of how to perform CRUD operations in Laravel with Firebase Realtime Database:

First, install the Firebase PHP SDK:

composer require firebase/php-jwt
composer require kreait/firebase-php

Next, you can use the following code to connect to Firebase Realtime Database:

use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;

$serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/service-account.json');
$firebase = (new Factory)
    ->withServiceAccount($serviceAccount)
    ->create();

$database = $firebase->getDatabase();

Note: Replace service-account.json with the path to your Firebase Service Account JSON file.

Create a new record:

$newPost = $database
    ->getReference('posts')
    ->push([
        'title' => 'My first post',
        'body' => 'Hello, Firebase!'
    ]);

Read a record:

$post = $database
    ->getReference('posts/'.$postId)
    ->getValue();

Note: Replace $postId with the actual ID of the post that you want to read.

Update a record:

$database
    ->getReference('posts/'.$postId)
    ->update([
        'title' => 'My updated post',
        'body' => 'Hello, updated Firebase!'
    ]);

Note: Replace $postId with the actual ID of the post that you want to update.

Delete a record:

$database
    ->getReference('posts/'.$postId)
    ->remove();

Note: Replace $postId with the actual ID of the post that you want to delete.

This is a basic example of how to perform CRUD operations in Laravel with Firebase Realtime Database. You can customize it to fit your needs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Upgrade PHP Version without using cPanel setting