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 editor and add below code into it into

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;  ?>

jQuery Datatable add date range filter

jQuery Datatable add date range filter Datatable is most useful jQuery plugin that helps to make our html tables more powerful and give powers to user to filter , search, sort, pagination etc, But Data table provides a common filter only and yes we can customize and add filter for each column, but still sometimes we need an advance filter like show results only between a date range, So today we will learn how to create a minimum and maximum date range fields and show date picker on it, and user can fill dates by selecting dates and data table will auto filter records based on it. Keep follow below steps :- I am using Bootstrap if you want to use any other framework then you can use. Create a new index.php file  and paste below code in it, i have used all required CDN like bootstrap, datatable, datepicker etc. <!DOCTYPE html> <html> <head>     <title>Datatable Date Range Filter Example</title>     <link rel="stylesheet" href="https://maxcd