1
votes

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.

1
Create one function and then put this code in it!Hiren Gohel
just remove in top use Config; because u are using config helper functionJignesh Joisar
@HirenGohel not workingJIJOMON K.A
@JigneshJoisar not workingJIJOMON K.A
@JIJOMONK.A: Please check my answer below!Hiren Gohel

1 Answers

0
votes

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