// 默認(rèn)連接mysql
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => database_path('database.sqlite'),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'database_name'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'root'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'mysql_test' => [
'driver' => 'mysql',
'host' => env('DB_HOST_TEST', '127.0.0.1'),
'port' => env('DB_PORT_TEST', '3306'),
'database' => env('DB_DATABASE_TEST', 'database_test'),
'username' => env('DB_USERNAME_TEST', 'root'),
'password' => env('DB_PASSWORD_TEST', 'root'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class Test extends Model
{
// 數(shù)據(jù)庫'database'中的test表
public $table = 'test';
public $timestamps = false;
// 以下代碼可有可不、默認(rèn)連接mysql
protected $connection = 'mysql';
}
a、這個model將采用默認(rèn)的'mysql'連接
class UserModel extends Model
{
// 數(shù)據(jù)庫'database'中的users表
protected $table = "users";
}
b、
// 連接mysql_test庫
DB::connection('mysql_test')->table('test')->where('id',1)->first();
// 連接mysql庫
DB::connection('mysq')->table('test')->where('id',1)->first();
// 連接mysql庫
DB::table('test')->where('id',1)->first();
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。