Home > Enterprise >  is it possible to autofill a form in a website with data from google sheets using Laravel or any pro
is it possible to autofill a form in a website with data from google sheets using Laravel or any pro

Time:12-29

I have created a Whatsapp chatbot using ManyChat that's ask the user to enter his name , Email and Phone number and after the user done with providing his info to the bot the user inputs will be saved to google sheets, and after that the chatbot will send the user a link to my website to finalize his request. this link will take him to a form with the same field as in whatsapp (name, Email and Phone Number).

i want the form to be prefilled with the unique data of the user when the user click the link and goes to the form in my website. I have already integrated my website with google sheets API. I just want to know if its possible and how to do it?

I want to try doing it using Token ID or Authentication ID but I Don't Know how to get it in my case

here's the code if you can find it helpful

Google Sheets Services

<?php

namespace App\Http\Services;

use Google\Client;
use Google\Service\Sheets;
use Google\Service\Sheets\Sheet;
use Google\Service\Sheets\ValueRange;

class GoogleSheetServices{
    public $client, $service, $documentId, $range;

    public function __construct()
    {
        $this->client= $this->getClient();
        $this->service= new Sheets($this->client);
        $this->documentId= '*my document id here*';
        $this->range='A:Z';
    }
    public function getClient(){
        $client=new Client();
        $client->setApplicationName('Google Sheet Demo');
        $client->setRedirectUri('http://localhost::8000/googlesheet');
        $client->setScopes(Sheets::SPREADSHEETS);
        $client->setAuthConfig('credentials.json');
        $client->setAccessType('offline');

        return $client;
    }
    public function readSheet(){

     $doc= $this->service->spreadsheets_values->get($this->documentId,$this->range);
     return $doc;
    }
}

Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Google\Service\Sheets;
use App\Http\Services\GoogleSheetServices;
class GoogleSheetController extends Controller
{
    //
    public function sheetOperation(Request $request){
        $data=(new GoogleSheetServices ())->readSheet();
        return view('googlesheet',compact('data'));
    }
}

Model

 <?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

Migration

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateGoogleOAuthsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('google_o_auths', function (Blueprint $table) {
            $table->id();
            $table->string('provider');
            $table->string('provider_value');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('google_o_auths');
    }
}

View

<!DOCTYPE html>
<html>

<head>
    <title>Contact form</title>
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css"
        integrity="sha384-5sAR7xN1Nv6T6 dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz" crossorigin="anonymous">
    <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700" rel="stylesheet">
    
</head>

<body>
    <div >
        <div >
            <i ></i>
            <i ></i>
            <i ></i>
        </div>
        @php
        foreach($data as $value){
        }
        @endphp
        <form action="/">
            <h1>Request for Multiple Qoutes</h1>
            <div >
                <select name="select_category" id="select_categor">
                    <option value="">Select Category</option>
                    <option value=>{{$value['0']}}</option>
                </select>
                <input  type="text" name="name" value={{$value['2']}} placeholder="Subject">
                <input type="text" name="name" placeholder="Email" value={{$value['3']}}>
                <input type="text" name="name" placeholder="Phone number" value={{$value['4']}}>
            </div>
            <p>Message</p>
            <div>
                <textarea rows="4">{{$value['1']}}</textarea>
            </div>
            <button type="submit" href="/">Submit</button>
        </form>
    </div>
</body>

</html>

here's the result of $sheetData = dd(new GoogleSheetServices())->readSheet();

enter image description here

CodePudding user response:

Yes for sure you can populate your view with data in multiple ways, and your, using controller is the common one, I would just recommend some organization in your controller like so:

$data = [];
$sheetData = (new GoogleSheetServices())->readSheet();

foreach ($sheetData as $value) {
    // Option 1: as Object
    $whatsAppUser = new \stdClass();
    $whatsAppUser->message = $value['1'];
    $whatsAppUser->name = $value['2'];
    $whatsAppUser->email = $value['3'];
    $whatsAppUser->phoneNumber = $value['4'];
    $data[] = $whatsAppUser;

    // Option 1: as Array
    $data[] = [
        'message' => $value['1'],
        'name' => $value['2'],
        'email' => $value['3'],
        'phoneNumber' => $value['4'],
    ];
}

return view('googlesheet', compact('data'));

If you are facing problems please update your answer with the result of dd((new GoogleSheetServices())->readSheet());

Edit: Based on your comment, in your googlesheet.blade file:

@foreach($data as $whatsAppUser)
<!-- if you choose option 1: -->
{{ $whatsAppUser->message }} 
{{ $whatsAppUser->name }}

<!-- if you choose option 2: -->
{{ $whatsAppUser['message'] }} 
{{ $whatsAppUser['name'] }}

@endforeach
  • Related