DB::connection() Laravel 5 not working - postgresql

I'am using Laravel 5.0 and I make 2 database which is one for system and two for data. when I try to change connection to get data from my second database it tell error
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "barang" does not
exist LINE 1: select * from barang
Here my Controller
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
class MultiController extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$data = DB::connection('pgsql2')->select('select * from barang');
return view('laporan.db')->withData($data);
}
Database.php
<?php
return [
'fetch' => PDO::FETCH_CLASS,
'default' => 'pgsql',
'connections' => [
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'larasimak'),
'username' => env('DB_USERNAME', 'postgres'),
'password' => env('DB_PASSWORD', 'postgres'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
'pgsql2' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'simkie_data'),
'username' => env('DB_USERNAME', 'postgres'),
'password' => env('DB_PASSWORD', 'postgres'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
my DotEnv
APP_ENV=local
APP_DEBUG=true
APP_KEY=nlXoLNFcWAD9rtTGXCUSpDdbQxms1ADi
DB_HOST=localhost
DB_DATABASE=larasimak
DB_USERNAME=postgres
DB_PASSWORD=postgres
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null

You should set the credential of second database. Current both database are using same credential. Try following for second database:
database.php
'pgsql2' => [
'driver' => 'pgsql',
'host' => env('PS2_DB_HOST', 'localhost'),
'database' => env('PS2_DB_DATABASE', 'simkie_data'),
'username' => env('PS2_DB_USERNAME', 'postgres'),
'password' => env('PS2_DB_PASSWORD', 'postgres'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
]
.evn
PS2_DB_HOST=localhost
PS2_DB_DATABASE=simkie_data
PS2_DB_USERNAME=postgres
PS2_DB_PASSWORD=postgres

Related

TYPO3 v9: how to query additional external database (MSSQL)

I am trying to query an additional external database connection in one of my repositories. In LocalConfiguration.php I've defined two connections (Default, External).
[...]
'DB' => [
'Connections' => [
// Local MySQL database
'Default' => [
// ...
],
// External MSSQL database
'External' => [
'charset' => 'utf-8',
'dbname' => 'DBNAME',
'driver' => 'sqlsrv',
'host' => 'someExternalIP',
'password' => 'somePassword',
'port' => 1433,
'user' => 'someUser',
],
],
],
[...]
In my repository I want to query the external database (via Doctrine).
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('dbo.SomeTable');
$queryBuilder->getRestrictions()->removeAll();
$queryBuilder
->select('*')
->from('dbo.SomeTable');
Do I have to explicitly tell the QueryBuilder to use that particular connection? Right now I am getting an Doctrine\DBAL\Exception\ConnectionException error, as the system tries to connect via the Default-Connection.
An exception occurred while executing 'SELECT * FROM `dbo`.`SomeTable`':
SELECT command denied to user 'myLocalUser'#'localhost' for table 'SomeTable'
Check out $GLOBALS['TYPO3_CONF_VARS']['DB']['TableMapping'] where you can explicitly define what tables are located in which database. See also this for some more details https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Database/Configuration/Index.html
The other option is actually to use ask the Connection by name, and create a query builder out of that.
GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionByName('External')->createQueryBuilder(...)
I personally would go with the latter, as it is more explicit within the actual callers code what is used.
To work with external DB, you have to :
configure the mapping with external database and table mapping in LocalConfiguration.php
define the TCA for external tables in myExt/Configuration/TCA/MyExternalTableName.php
configure the external tables/columns mapping in ext_typoscript_setup.txt
and then, the queries in repositories will work.
Sample LocalConfiguration.php :
'DB' => [
'Connections' => [
'Default' => [
'charset' => 'utf8',
'dbname' => 'LOCAL-DB',
'driver' => 'mysqli',
'host' => '127.0.0.1',
'password' => 'PWD',
'port' => 3306,
'user' => 'USER',
],
'externalDb' => [
'charset' => 'utf8',
'dbname' => 'EXTERNAL-DB',
'driver' => 'mysqli',
'host' => 'localhost',
'password' => 'PWD',
'port' => 3306,
'user' => 'USER',
],
],
'TableMapping' => [
'MyexternalTable1' => 'externalDb',
'MyexternalTable2' => 'externalDb',
...
]
]
Sample columns mapping in myExt/ext_typoscript_setup.txt :
plugin.tx_myext {
persistence {
classes {
Vendor\MyExt\Domain\Model\LocalModel {
mapping {
tableName = ExternalTableName
recordType = \Vendor\MyExt\Domain\Model\LocalModel
columns {
col1.mapOnProperty = uid
col2.mapOnProperty = name
...
}
}
}
}
}
}

Can't send email with laravel 5.1 + gmail

I'm having troubles sending e-mails via a Laravel 5.1 application using gmail.
Here is my config file :
<?php
return [
'driver' => 'sendmail',
'host' => 'smtp.gmail.com',
'port' => 578,
'from' => array('address' => 'adresse#gmail.com', 'name' => 'Something'),
'encryption' => 'tls',
'username' => 'someaddress#gmail.com',
'password' => '****',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
Allowing less secure apps to access your account is turned on.
What config shall I use ?
You must to allow less secure apps here and Enable Unlock Captcha here
Also, Set .env as follows
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=something#gmail.com
MAIL_PASSWORD=*********
MAIL_ENCRYPTION=tls
And Set config/mail.php as follows
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => 'something#gmail.com',
'name' => env('MAIL_FROM_NAME', 'Admin'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME','something#gmail.com'),
'password' => env('MAIL_PASSWORD','*********'),
'sendmail' => '/usr/sbin/sendmail -bs',
Hope it's Help!

Lumen connecting to multiple databases using DB_ connections in .env - Possible?

I can see how to have multiple connections using a configuration file in app/config/database.php and this is well documented.
Is this the only way or can you also define multiple connections using the .env file?
First, you'll need to configure your connections. You need to create a config directory in your project and add the file config/database.php. Like this:
<?php
return [
'default' => 'mysql',
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'homestead',
'username' => 'root',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'mysql2' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'homestead2',
'username' => 'root',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
Once you've added your connection configurations, you can access them by getting the database manager object out of the container and calling ->connection('connection_name'). See below for a full example.
<?php
namespace App\Http\Controllers;
use Illuminate\Database\DatabaseManager;
class StatsController extends Controller
{
/**
* #return array
*/
public function getLatest()
{
// Resolve dependencies out of container
/** #var DatabaseManager $db */
$db = app('db');
$database1 = $db->connection('mysql');
$database2 = $db->connection('mysql2');
// Look up 3 newest users and 3 newest blog posts
$threeNewestUsers = $database1->select("SELECT * FROM users ORDER BY created_at DESC LIMIT 3");
$threeLatestPosts = $database2->select("SELECT * FROM blog_posts ORDER BY created_at DESC LIMIT 3");
return [
"new_users" => $threeNewestUsers,
"new_posts" => $threeLatestPosts,
];
}
}
http://andyfleming.com/configuring-multiple-database-connections-in-lumen-without-facades/

Not working email send in Laravel on live server

It is working on my localhost . But on live server not working.
It shows following error
Connection could not be established with host smtp.gmail.com [Connection timed out #110]
How to solve it?
And my app/config/mail.php
<?php
return array(
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 465,
'from' => array('address' => 'myaddress', 'name' => 'Name'),
'encryption' => 'ssl',
'username' => 'myemail#gmail.com',
'password' => 'mypassword',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
);
Try this configuration:
return array(
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 587,
'from' => array('address' => 'myaddress', 'name' => 'Name'),
'encryption' => 'tls',
'username' => 'myemail#gmail.com',
'password' => 'mypassword',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
);
Hope this will work for both your local as well as live server.
It will work in laravel 5.1
In your .env file
change the follwoing
MAIL_DRIVER=mail
MAIL_HOST=www.yourhost.com
MAIL_PORT=587
MAIL_USERNAME=your email address
MAIL_PASSWORD=email password
MAIL_ENCRYPTION=null
change in config/mail.php
return [
'driver' => env('MAIL_DRIVER', 'mail'),
'host' => env('MAIL_HOST', 'www.yourhostname.com'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => 'youremail#domain.com', 'name' => 'any name'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];

Doctrine ODM for MongoDB and Zend Framework 2

Where is my mistake in the configuration?
I try to implement Doctrine ODM in ZF2 - Have a look to my Configuration File:
namespace Application;
return array(
'doctrine' => array(
'connection' => array(
'odm_default' => array(
'server' => 'localhost',
'port' => '27017',
'user' => null,
'password' => null,
'dbname' => 'homeup',
'options' => array()
),
),
'configuration' => array(
'odm_default' => array(
'metadata_cache' => 'array',
'driver' => 'odm_default',
'generate_proxies' => true,
'proxy_dir' => 'data/DoctrineMongoODMModule/Proxy',
'proxy_namespace' => 'DoctrineMongoODMModule\Proxy',
'generate_hydrators' => true,
'hydrator_dir' => 'data/DoctrineMongoODMModule/Hydrator',
'hydrator_namespace' => 'DoctrineMongoODMModule\Hydrator',
'default_db' => null,
'filters' => array(), // array('filterName' => 'BSON\Filter\Class'),
'logger' => null // 'DoctrineMongoODMModule\Logging\DebugStack'
)
),
'driver' => array(
'odm_default' => array(
/* 'drivers' => array(
'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
'namespace' => 'Application\Document',
'paths' => array('module/Application/src/Application/Document'),
*/
'odm_driver' => array(
'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
'paths' => array(__NAMESPACE__ .'/src/' . __NAMESPACE__ . '/Document')
),
'odm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Document' => 'odm_driver'
)
)
)
)
),
'documentmanager' => array(
'odm_default' => array(
'connection' => 'odm_default',
'configuration' => 'odm_default',
'eventmanager' => 'odm_default'
)
),
'eventmanager' => array(
'odm_default' => array(
'subscribers' => array()
)
),
);
Info:
Used tutorial : https://github.com/doctrine/DoctrineMongoODMModule
ZF2 Version 2.2.4
Doctrine dev-master
MongoDB PHP Driver works (with plain PHP)
Wastet hours of time & searched the web
EDIT
Fatal error: Uncaught exception
'Zend\Stdlib\Exception\BadMethodCallException' with message 'The
option "odm_driver" does not have a matching setOdmDriver setter
method which must be defined' in
C:\xampp\htdocs\homeup.dev\vendor\zendframework\zendframework\library\Zend\Servi‌​ceManager\ServiceManager.php
on line 859
It was easier than I thought...
Solution is here:
example working config for Doctrine ODM zf2 Module?
Less is more.