【发布时间】:2020-01-10 03:48:50
【问题描述】:
我在 MySql 中有一个处于本地模式的数据库,另一个数据库在 MariaDB 的另一台服务器中
我在我的 database.php 文件中对它们进行了如此多的配置 就像在 .env 中一样
database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'test'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'modes' => [
'ONLY_FULL_GROUP_BY',
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_ENGINE_SUBSTITUTION',
],
],
'asterisk' => [
'driver' => 'mysql',
'host' => env('ASTERISK_HOST', 'localhost'),
'database' => env('ASTERISK_DATABASE', 'forge'),
'username' => env('ASTERISK_USERNAME', 'forge'),
'password' => env('ASTERISK_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3307
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=''
ASTERISK_HOST=192.168.2.212
ASTERISK_PORT=3306
ASTERISK_DATABASE=database
ASTERISK_USERNAME=usuario
ASTERISK_PASSWORD='password'
我测试连接的测试路径是Route::get('vicidial','Vicidial\VicidialPruebaController@index');
控制器如下
Vicidial\VicidialPruebaController.php
<?php
namespace App\Http\Controllers\Vicidial;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\VicidialModel\VicidialList;
class VicidialPruebaController extends Controller
{
public function index(){
$list = VicidialList::find(1);
dd($list);
}
}
模型是
VicidialModel\VicidialList.php
<?php
namespace App\VicidialModel;
use Illuminate\Database\Eloquent\Model;
class VicidialList extends Model{
/**
* The connection name for the model.
*
* @var string
* @author Luis Morales
*/
protected $connection = 'asterisk';
/**
* The table associated with the model.
*
* @var string
* @author Luis Morales
*/
protected $table = 'vicidial_list';
/**
* The primary key associated with the table.
*
* @var string
* @author Luis Morales
*/
protected $primaryKey = 'lead_id';
/**
* The attributes that are mass assignable.
*
* @var array
* @author Luis Morales
*/
protected $fillable = [
'entry_date',
'modify_date',
'status',
'user',
'vendor_lead_code',
'source_id',
'list_id',
'gmt_offset_now',
'called_since_last_reset',
'phone_code',
'phone_number',
'title',
'first_name',
'middle_initial',
'last_name',
'address1',
'address2',
'address3',
'city',
'state',
'province',
'postal_code',
'country_code',
'gender',
'date_of_birth',
'alt_phone',
'email',
'security_phrase',
'comments',
'called_count',
'last_local_call_time',
'rank',
'owner',
'entry_list_id'
];
/**
* Indicates if the model should be timestamped.
*
* @var bool
* @author Luis Morales
*/
public $timestamps = false;
}
连接尝试出错,因为连接方一段时间后没有正确响应,或者连接的主机无法正常连接,建立连接出错
这是对第二个数据库 Asterisk 的第一次测试。有了第一个,我的系统就可以正常工作了
【问题讨论】:
-
能否请您编辑问题并添加英文错误?
-
在块引用中是英文的错误,在图片上方。
标签: mysql laravel mariadb laravel-5.8