Skip to main content

Integrate firebase database with laravel and save functionality

 How to Integrate firebase database with laravel and save functionality

In this tutorial, we will use default firebase database so first login to google console and go to project settings -> service account adn click on generate new private key and download it and put it in root folder of laravel app



1) Install Google Cloud Firestore via Composer:

composer require google/cloud-firestore


2) Add Firebase Credentials to .env file: Ensure you have added the following in your .env file:
FIREBASE_CREDENTIALS=./firebase_credentials.json
FIREBASE_PROJECT_ID=mydb

3) Create a Firestore Service Provider: You need to create a service provider to configure Firestore in your Laravel app. Run the following command to generate a service provider:

php artisan make:provider FirestoreServiceProvider

4) Configure Firestore in the Service Provider: Open the newly created FirestoreServiceProvider.php file and configure Firestore:

<?php

namespace App\Providers;

use Google\Cloud\Firestore\FirestoreClient;
use Illuminate\Support\ServiceProvider;

class FirestoreServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton(FirestoreClient::class, function ($app) {
            $config = [
                'projectId' => env('FIREBASE_PROJECT_ID'),
                'keyFilePath' => base_path(env('FIREBASE_CREDENTIALS')),
            ];

            return new FirestoreClient($config);
        });
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}


5) Register the Service Provider: Add the newly created service provider to the providers array in config/app.php:
'providers' => [
    // Other service providers...
    App\Providers\FirestoreServiceProvider::class,
],


6) Using Firestore in Your Application: Now you can use Firestore in your controllers or services. For example, to save data to the inquiries collection, you can do the following in a controller:

<?php

namespace App\Http\Controllers;

use Google\Cloud\Firestore\FirestoreClient;
use Illuminate\Http\Request;

class InquiryController extends Controller
{
    protected $firestore;

    public function __construct(FirestoreClient $firestore)
    {
        $this->firestore = $firestore;
    }

    public function store(Request $request)
    {
        $data = [
            'name' => $request->input('name'),
            'email' => $request->input('email'),
            'message' => $request->input('message'),
            'created_at' => now(),
        ];

        $this->firestore->collection('inquiries')->add($data);

        return response()->json(['success' => 'Inquiry saved successfully.']);
    }
}


7) Define a Route: Add a route to handle the inquiry submission in routes/web.php or routes/api.php:

use App\Http\Controllers\InquiryController; Route::post('/inquiries', [InquiryController::class, 'store']);



thats it ! now access the url

Comments

Popular posts from this blog

Run and compile sass scss file to css using node

  Today we learn how to use scss and generate css using node  or Run and compile sass scss file to css using node   So please follow simple  steps :-   Today we will create a project that can read scss file and generates css with it  Note: Make sure you have installed node in your system. If you want to help to install node js based on your system then check our other tutorial or check node js official website. Now create a blank folder and open  terminal(linux) or cmd(windows) and navigate to your current project folder by using cd command Now run below command npm init after enter it will ask you some package info that you can fill according to you or just keep enter until it finished. The above command will generate package.json file Now  we will install npm module that will convert our scss to css Run below command: npm install node-sass So we have installed node-sass package . Now open package.json file in your...

How to retrieve Facebook Likes, share , comment Counts

function facebook_count($url){     // Query in FQL     $fql  = "SELECT share_count, like_count, comment_count ";     $fql .= " FROM link_stat WHERE url = '$url'";     $fqlURL = "https://api.facebook.com/method/fql.query?format=json&query=" . urlencode($fql);     // Facebook Response is in JSON     $response = file_get_contents($fqlURL);     return json_decode($response); } $fb = facebook_count('https://www.facebook.com/BahutHoGyiPadhai'); // facebook share count echo $fb[0]->share_count;  echo "like"; // facebook like count echo $fb[0]->like_count ; echo "comment"; // facebook comment count echo $fb[0]->comment_count;  ?>

Solution-windows 'expo' is not recognized as an internal or external command

Solution for expo is not recognized as an internal or external command,operable program or batch file in Windows 10 Sometimes expo will not work globally mostly in windows 10, If you are facing same issue then follow the below Steps 1) Click on windows button and search for  " Environment variables"  and click on "Edit the system environment variables" 2) Now you will see a popup like below screen. Then you need to click on Environment Variables. (Please see highlight part in below image)     3)Then click on new button that i have highlighted in below image 4. Then a popup will open and you need to fill details like below mentioned Variable Name :Path Variable Value: %USERPROFILE%\AppData\Roaming\npm Here we are creating a new path variable and passing location of npm.   Now Click on OK and close all the terminal windows and open new CMD or terminal and type Expo . Great now you can access expo from any...