1

In my .env file I have two variables

App_id: 12345
App_secret: abc123

But I'm wondering if there's a way so that if user userNo2 logs in then it would instead use

App_id: 45678
App_secret: abc456

Is there a way to have if/else functionality in the env file based on the user?

miken32
  • 42,008
  • 16
  • 111
  • 154
Geoff_S
  • 4,917
  • 7
  • 43
  • 133
  • No, you cant. You should using DB for each user. `.env file` just used when change value between develop and production environment – Ryan Nghiem Jun 17 '19 at 02:40
  • if you are trying this condition try a package for store value and package name is `STORAGE` because if you are login to store value in storage and get value only one time this package use cache and store value in cache one time goes to db and other time get value from cache try this – Developer Jun 17 '19 at 04:22
  • this is a perfect scenario for adding additional columns to the `users` table and storing that info there. – Erich Jun 17 '19 at 05:04

2 Answers2

3

Yes it is possible, but not in the .env file. Instead, you can move your logic to middleware:

Step 1: Add default values to the application config

Open your app/config/app.php and add your default values to the existing array.

<?php

return [
    'APP_ID' => '45678',
    'APP_SECRET' => 'abc456',
    ...
];

Step 2: Create a new Middleware

php artisan make:middleware SetAppConfigForUserMiddleware

Edit the file to look like this:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;

class SetAppConfigForUserMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
         $authorizedUser = Auth::user();

        if (!App::runningInConsole() && !is_null($authorizedUser)) {
            Config::set('app.APP_ID', 'appidOfUser' . $authorizedUser->name);
            Config::set('app.APP_SECRET', 'appsecretOfUser' . $authorizedUser->email);
        }
        return $next($request);
    }
}

Step 4: Run your middleware

If you need to set this config for the user in all the web routes you can add to the $middlewareGroups array in app/Http/kernel.php. This will apply the middleware to all the routes inside web.php.

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        ...
        \App\Http\Middleware\SetAppConfigForUserMiddleware::class,        
    ],

Step 5: Testing

For example, my Auth:user()->name is "John" and my Auth:user()->email is "john@example.com"

If you put this in your resources/views/home.blade.php

App Id Of User <code>{{config('app.APP_ID')}}</code>
App Secret Of User <code>{{config('app.APP_SECRET')}}</code>

The result will be appidOfUserJohn and appsecretOfUserjohn@example.com.

miken32
  • 42,008
  • 16
  • 111
  • 154
ManojKiran A
  • 5,896
  • 4
  • 30
  • 43
  • 1
    I've been looking for a straight answers for changing config on runtime for a few days now!, thanks for this post! – Bart Mommens Feb 14 '21 at 10:19
0

.env can only store key-value.

Since .env is always used by config, you can use Config::set('app.id', 45678); to mutate the env at run time. You can place the code in your middleware, and the value will back to default after the request ends.

Cloud Soh Jun Fu
  • 1,456
  • 9
  • 12