我有一个用Laravel构建的系统,它连接在两个数据库中,一个用mysql,另一个用mssql。我的默认连接是mysql:
'default' => 'mysql',
和
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'qdf_api',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => '192.168.3.248,51433',
'database' => 'sample_test',
'username' => 'joene',
'password' => 'password',
'charset' => 'utf8',
'prefix' => '',
],
然后我的控制器:
public function store(Request $request)
{
Validator::extend('unique_multiple', function ($attribute, $value, $parameters)
{
// Get table name from first parameter
$table = array_shift($parameters);
// Build the query
$query = DB::table($table);
// Add the field conditions
foreach ($parameters as $i => $field)
$query->where($field, $value[$i]);
// Validation result will be false if any rows match the combination
return ($query->count() == 0);
});
$file = Input::file('file');
$count = 0;
Excel::load($file, function($reader) use ($count) {
$results = $reader->get()->toArray();
// Set connection to sqlsrv
$verifier = App::make('validation.presence');
$verifier->setConnection('sqlsrv');
foreach ($results as $key => $value) {
$validator = Validator::make(
// Validator data goes here
array(
'unique_fields' => array($value['telephone'], $value['columnheadername'])
),
// Validator rules go here
array(
'unique_fields' => 'unique_multiple:Deliveries,Telephone,ColumnHeaderName',
'file' => 'required'
)
);
$validator->setPresenceVerifier($verifier);
if ($validator->fails())
{
return Redirect::to('delivery/create')->withErrors($validator);
}
else
{
$deliveries = new Delivery();
$deliveries->Title = $value['title'];
$deliveries->Firstname = $value['firstname'];
$deliveries->Surname = $value['surname'];
$deliveries->Address1 = $value['address1'];
$deliveries->Address2 = $value['address2'];
$deliveries->Address3 = $value['address3'];
$deliveries->Address4 = $value['address4'];
$deliveries->Town = $value['town'];
$deliveries->County = $value['county'];
$deliveries->Postcode = $value['postcode'];
$deliveries->Telephone = $value['telephone'];
$deliveries->Email = $value['email'];
$deliveries->AgeBracket = $value['agebracket'];
$deliveries->Gender = $value['gender'];
$deliveries->Filename = $value['filename'];
$deliveries->CalledDate = $value['calleddate'];
$deliveries->ColumnHeaderName = $value['columnheadername'];
$deliveries->Answer1 = $value['answer1'];
$deliveries->Answer2 = $value['answer2'];
$deliveries->Answer3 = $value['answer3'];
$deliveries->Answer4 = $value['answer4'];
$deliveries->Answer5 = $value['answer5'];
$deliveries->HomeOwner = $value['residentialstatus'];
$deliveries->MaritalStatus = $value['maritalstatus'];
$deliveries->WorkStatus = $value['incomebracket'];
if($deliveries->save()){$count++;}
}
}
Session::flash('alert-success', 'Data Uploaded Successfully! '.$count.' leads uploaded');
});
return Redirect::to('delivery/create');
}
你可以看到这一行:
$verifier = App::make('validation.presence');
$verifier->setConnection('sqlsrv');
仍然有这个错误:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'qdf_api.deliveries' doesn't exist (SQL: select count(*) as aggregate from `Deliveries` where `Telephone` = 01482828164 and `ColumnHeaderName` = EDF_3320)
交付表位于我的sample_test
数据库中,该数据库位于sqlsrv
连接中。这就是为什么我切换到sqlsrv
连接,但似乎无法正常工作?
答案 0 :(得分:0)
两个连接都没有相同的数据库名称,并且您可能在.env文件中添加了 qdf_api 数据库名称。因此你会收到错误。