Laravel在模型中设置连接数据库不起作用

时间:2018-04-04 01:30:47

标签: laravel model database-connection laravel-5.4

我已经在我的模型中声明了我的新连接,但它无效, 附在这里是我的模特和控制器。

模型

protected $connection = 'mysql';

public function __construct() {
    $this->connection = 'newConnection'.auth()->user()->company_id;
}

protected $fillable =['operation_id','warehouse_id','salesman_name','address','contact_number','email_address'];

public $primaryKey='id';

控制器

 $salesman = new Salesman([
                'operation_id'   => $request->get('operation_id'),  
                'warehouse_id'   => $request->get('warehouse_id'),
                'salesman_name'  => $request->get('salesman_name'),  
                'address'        => $request->get('address'),  
                'contact_number' => $request->get('contact_number'),  
                'email_address'  => $request->get('email_address'),
            ]);
$salesman->save();

3 个答案:

答案 0 :(得分:1)

我认为你正在覆盖构造方法中的var myName = 'Sophia'; console.log('Hello my name is ' + myName + '.'); // No comma - one expression to evaluate console.log('Hello my name is', myName, '.'); // With commas - 3 expressions to evaluate console.log('Hello my name is', myName + '.'); // Combination - 2 expressions to evaluate变量。连接名称根据公司ID更改。确保指定的数据库连接在$connection中定义为 -

config/database.php

编辑: 如果插入不起作用,请尝试:

'connections' => [
    'sqlite' => [
        'driver' => 'sqlite',
        'database' => env('DB_DATABASE', database_path('database.sqlite')),
        'prefix' => '',
    ],
    'newConnectionCompanyId' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

答案 1 :(得分:0)

您可能会误解db连接配置,下面是一个完整的示例:

<强>模型

protected $connection = 'mysql_1';

<强>配置/ database.php中

'connections' => [

        'mysql_1' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST_1', 'localhost'),
            'port' => env('DB_PORT_1', '3306'),
            'database' => env('DB_DATABASE_1', 'database_1'),
            'username' => env('DB_USERNAME_1', 'root'),
            'password' => env('DB_PASSWORD_1', 'root'),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

        'mysql_2' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST_2', 'localhost'),
            'port' => env('DB_PORT_2', '3306'),
            'database' => env('DB_DATABASE_2', 'database_2'),
            'username' => env('DB_USERNAME_2', 'root'),
            'password' => env('DB_PASSWORD_2', 'root'),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

        ...

<强> .ENV

//DB_CONNECTION is default connection
DB_CONNECTION=mysql_1

DB_HOST_1=localhost
DB_PORT_1=3306
DB_DATABASE_1=database_1
DB_USERNAME_1=username_1
DB_PASSWORD_1=password_1

DB_HOST_2=localhost
DB_PORT_2=3306
DB_DATABASE_2=database_2
DB_USERNAME_2=username_2
DB_PASSWORD_2=password_2

答案 2 :(得分:0)

模型

protected $connection = 'mysql_1';

确保您不要使用

DB::table('{table_name}')->where(...

相反,将主表名称定义为

protected $table = 'table_name';

并使用Model的名称编写查询

Model_Name::where(...