0

I am trying to implement Gmail API for CRM based on laravel, where users can store multiple Google credentials, and using those credentials users can log in with their Google account.

I used dacastro4 laravel-gmail package, but for dacastro4/laravel-gmail package by default design, those Google credentials are stored in .env file of the laravel project.

.env

GOOGLE_PROJECT_ID=
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_REDIRECT_URI=

`

I tried setting the .env variable in the controller constructor function, but not working. for example,

env('GOOGLE_PROJECT_ID',$project_id);
//OR
putenv("GOOGLE_PROJECT_ID=".$project_id);
//OR
config(['GOOGLE_PROJECT_ID' => $project_id])

Also tried setting in the vendor dacastro4 laravel-gmail package, but the database model is not accessible.

How can I set multiple Google credentials from the controller?

Thank You.

Danon
  • 2,771
  • 27
  • 37
codewiz
  • 493
  • 1
  • 7
  • 20
  • 1
    you wouldn't set the env variables you would set a config value ... it pulls the env variables for the config at `gmail.php` ... https://github.com/dacastro4/laravel-gmail/blob/master/src/config/gmail.php those are the config values ... the config file should have been published to your `config` folder for reference – lagbox Nov 19 '22 at 15:10

1 Answers1

2

You can set this data using the config() method, seeing as that's how Laravel accesses .env variables.

Create a config file for your variables:

config/gmail.php

<?php

return [
    'project_id' => env('GOOGLE_PROJECT_ID'),
    'client_id' => env('GOOGLE_CLIENT_ID'),
    'client_secret' => env('GOOGLE_CLIENT_SECRET'),
    'redirect_url' => env('GOOGLE_REDIRECT_URI', '/'),
]

Then set values in your controller on the go using:

config(['gmail.project_id' => $project_id]);

and retrieve the values using:

config('gmail.project_id');
Delaney
  • 21
  • 1
  • 4