Using Laravel to Add Google Sheet Tutorial
Dec 21, 2022 . Admin
![](/upload/blog/using-laravel-to-add-google-sheet-tutorial.png)
Hello Friends,
We will discover how to add a Google Sheet using Laravel in this article. You've come to the correct spot if you want to see an example of how I added Google Sheets using Laravel. Adding a sheet to a Google Sheet using Laravel is demonstrated here simply. You can get a general idea of how I upload data automatically to Google Sheets. You only need to complete a few steps while using Laravel. Adding a Google sheet: how to do it.
In this example, Laravel will be used to add a Google Sheet. Google Sheet Add in Laravel. So let's explore the next steps:
So let's follow the below steps:
Step 1: Install LaravelThis is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:
composer create-project laravel/laravel example-appStep 2: Installation of revolution/laravel-google-sheets
On an installation of Laravel, run:
composer require revolution/laravel-google-sheets
Followed by:
php artisan vendor:publish --provider="PulkitJalan\Google\GoogleServiceProvider" --tag="config"Step 3: Set up Google credentials (API Key)
Go to https://developers.google.com/console and sign in (if necessary)
Create a new project (if necessary)
![](/upload/blog/google-sheet-api-1.png)
![](/upload/blog/google-sheet-api-2.png)
Click on “credentials” from the left-hand menu.
![](/upload/blog/google-sheet-api-3.png)
Click “Create credentials” followed up “OAuth client ID”.
![](/upload/blog/google-sheet-api-4.png)
![](/upload/blog/google-sheet-api-5.png)
If necessary, “Configure consent screen”.
![](/upload/blog/google-sheet-api-6.png)
select "External" and create.
![](/upload/blog/google-sheet-api-7.png)
Add Your Email in "User support email" and "Developer contact information" thane save and continue it.
![](/upload/blog/google-sheet-api-8.png)
![](/upload/blog/google-sheet-api-9.png)
Scopes are optional (if necessary).
![](/upload/blog/google-sheet-api-10.png)
Test users are optional (if necessary).
![](/upload/blog/google-sheet-api-11.png)
Summary is optional (if necessary).
![](/upload/blog/google-sheet-api-12.png)
![](/upload/blog/google-sheet-api-13.png)
After publishing your app go back to credentials.
![](/upload/blog/google-sheet-api-14.png)
![](/upload/blog/google-sheet-api-15.png)
Set the type to “Web application”.
![](/upload/blog/google-sheet-api-16.png)
![](/upload/blog/google-sheet-api-17.png)
Copy the “Client ID” (leave this window open)
Open /.env and paste the ID next to GOOGLE_CLIENT_ID=
Open /config/google.php and paste the ID into 'client_id' => env('GOOGLE_CLIENT_ID', '')
Go back to the Google Console and copy the “Client Secret”
Open /.env and paste the ID next to GOOGLE_CLIENT_SECRET=
Open /config/google.php and paste the ID into 'client_secret' => env('GOOGLE_CLIENT_SECRET', '')
![](/upload/blog/google-sheet-api-17(1).png)
Go back to the developer console and create a new “Service account key”.
![](/upload/blog/google-sheet-api-18.png)
![](/upload/blog/google-sheet-api-19.png)
Select a service account (create a new one if necessary – Role > Project > Owner).
![](/upload/blog/google-sheet-api-20.png)
![](/upload/blog/google-sheet-api-21.png)
![](/upload/blog/google-sheet-api-22.png)
Grant users access to this service account is optional (if necessary).
![](/upload/blog/google-sheet-api-23.png)
Copy your Service Account's Email and go to "Manage service accounts".
![](/upload/blog/google-sheet-api-24.png)
click on Actions and select "Manage Key".
![](/upload/blog/google-sheet-api-25.png)
go to "Add Key" and "Crete New Key".
![](/upload/blog/google-sheet-api-26.png)
Key Type should be “JSON”.
![](/upload/blog/google-sheet-api-27.png)
Save the .json file under the project’s /storage/ folder as “credentials.json” (and upload to the server if applicable).
![](/upload/blog/google-sheet-api-28.png)
Return to the main Developer Dashboard https://developers.google.com/console.
Click "Enable APIs and Services".
![](/upload/blog/google-sheet-api-29.png)
In the search box, type “drive api” and click on the “Google Drive API” option.
![](/upload/blog/google-sheet-api-30.png)
Click “ENABLE”.
![](/upload/blog/google-sheet-api-31.png)
Search for “sheets api” and click on “Google Sheets API”.
![](/upload/blog/google-sheet-api-32.png)
Click “ENABLE”.
![](/upload/blog/google-sheet-api-33.png)
http://sheets.google.comOpen and create a new document.
![](/upload/blog/google-sheet-api-34.png)
Give your new Sheet a name, Give the specific sheet (tab) a name, and Copy the ID of the document from the URL.
![](/upload/blog/google-sheet-api-35.png)
Copy your Service Account's Email.
![](/upload/blog/google-sheet-api-36.png)
Go back to your Sheet Click on “Share” Paste the email address for the service account and make sure to give it “edit” access.
![](/upload/blog/google-sheet-api-37.png)
GOOGLE_SERVICE_ENABLED=true
GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION=../storage/credentials.jsonStep 8: some change in google.php config/google.php
Add following code in scopes
/* |---------------------------------------------------------------------------- | Google OAuth 2.0 access |---------------------------------------------------------------------------- | | Keys for OAuth 2.0 access, see the API console at | https://developers.google.com/console | */ 'client_id' => env('GOOGLE_CLIENT_ID', ''), 'client_secret' => env('GOOGLE_CLIENT_SECRET', ''), 'redirect_uri' => env('GOOGLE_REDIRECT', ''), 'scopes' => [\Google\Service\Sheets::DRIVE, \Google\Service\Sheets::SPREADSHEETS], 'access_type' => 'online', 'approval_prompt' => 'auto',
pass the path of credentials.json in the file path
/* |---------------------------------------------------------------------------- | Google service account |---------------------------------------------------------------------------- | | Set the credentials JSON's location to use assert credentials, otherwise | app engine or compute engine will be used. | */ 'service' => [ /* | Enable service account auth or not. */ 'enable' => env('GOOGLE_SERVICE_ENABLED', false), /* * Path to service account json file. You can also pass the credentials as an array * instead of a file path. */ 'file' => storage_path('credentials.json'), ],Step 9: Create Route
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\GoogleSpreadsheetController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('addsheet-google-api', [GoogleSpreadsheetController::class, 'addsheet']);Step 10: Create Controller app/Http/Controllers/GoogleSpreadsheetController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Sheets; class GoogleSpreadsheetController extends Controller { /** * Write code on Method * * @return response() */ public function addsheet() { Sheets::spreadsheet('1_RmzVf7sGXZj1Y1U_DFbGHQzq6qOxJtmVmeS8XIgGOs')->addSheet('New_dexGroupe2'); dd('New Sheet Added'); } }Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serve
Now, Go to your web browser, type the given URL and view the app output:
http://localhost:8000/addsheet-google-api
I hope it can help you...