1

I tried to change the database connection from laravel model,

This is my code,

namespace App\model;

use Illuminate\Database\Eloquent\Model;

use Config;
use DB;
class Process extends Model
{

    config(['database.connections.newsql' => [
            'host' => '127.0.0.1',
            'password' => '',
            'database' => 'gps_bizspecific',
            'username' => 'root'
        ]]);

But I got an error

syntax error, unexpected 'config' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)

How to solve this ???

NOTE : I dont want to update config/database.php., I need to pass the database name (stored in a section variable) from model

Situation :

When a user register in my site it will create a dedicated database for each user, so when each user login to my site i need to fetch datas from corresponding user database

I have a central database with all user details and database names, when a user login I fetch the datatabse name from the central database and stored it into a session variable. after that I need to create a connection with that new database

Thank you.

JIJOMON K.A
  • 1,290
  • 3
  • 12
  • 29
  • Create one function and then put this code in it! – Hiren Gohel Dec 13 '18 at 05:47
  • just remove in top `use Config;` because u are using config helper function – Jignesh Joisar Dec 13 '18 at 05:49
  • @HirenGohel not working – JIJOMON K.A Dec 13 '18 at 05:52
  • @JigneshJoisar not working – JIJOMON K.A Dec 13 '18 at 05:52
  • @JIJOMONK.A: Please check my answer below! – Hiren Gohel Dec 13 '18 at 05:53
  • @JIJOMONK.A: Can you please let us know why you want to pass database name? What's your usage of it for further? – Hiren Gohel Dec 13 '18 at 06:09
  • @HirenGohel Sure, When a user register in my site it will create a dedicated database for each user, so when each user login to my site i need to fetch datas from corresponding user database – JIJOMON K.A Dec 13 '18 at 06:13
  • But this is not a good idea to create a database for each user! You can manage it in one database as well and get their data from user id! Think if there's thousands of user and how can you manage all database for all of the users? – Hiren Gohel Dec 13 '18 at 06:16
  • @HirenGohel Its a huge Project , One single database contain more than 100 tables and it is hosted in different countries, so I can't manage the all datas with a single database thats why.......... – JIJOMON K.A Dec 13 '18 at 06:20
  • You can get database name using: `\DB::getDatabaseName();` and then return it so that you can get it in your controller OR you can directly use it in your controller. – Hiren Gohel Dec 13 '18 at 06:23
  • @HirenGohel I have a central database with all user details and database names, when a user login I fetch the datatabse name from the central database and stored it into a session variable. after that I need to create a connection with that new database – JIJOMON K.A Dec 13 '18 at 06:26
  • Ok, please see this one tutorial: https://lukevers.com/2015/03/25/on-the-fly-database-connections-with-laravel-5 – Hiren Gohel Dec 13 '18 at 06:29

1 Answers1

0

Just simply enter the db details in .env file

 DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306
 DB_DATABASE=yourdbname DB_USERNAME=yourusername DB_PASSWORD=password

then enter below command

php artisan config:cache
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Prathamesh Doke
  • 797
  • 2
  • 16
  • 38