Eloquent outside laravel change fetch mode to PDO::FETCH_ASSOC - php

I'm using Eloquent outside laravel,
and here's my code for initializing Eloquent,
but I wasn't able to change fetch mode to PDO::FETCH_ASSOC,
$capsule->addConnection([
'driver' => 'mysql',
'host' => env('Database_Host'),
'database' => env('Database_Name'),
'username' => env('Database_Username'),
'password' => env('Database_Password'),
'charset' => 'utf8',
'collation' => 'utf8_persian_ci',
'prefix' => '',
]);
$capsule->setFetchMode(PDO::FETCH_ASSOC);
// $capsule->getConnection()->getPdo()->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
// $capsule->setAsGlobal();
return $capsule->getConnection();

Are you trying to do this for every query or just one? For all try:
$capsule->addConnection([
'driver' => 'mysql',
'host' => env('Database_Host'),
'database' => env('Database_Name'),
'username' => env('Database_Username'),
'password' => env('Database_Password'),
'charset' => 'utf8',
'collation' => 'utf8_persian_ci',
'prefix' => '',
'fetch' => PDO::FETCH_ASSOC
]);
For a single query try:
$query->fetchAll(PDO::FETCH_ASSOC)

Related

SQLSTATE[HY000]: [Microsoft][ODBC Driver 11 for SQL Server]TDS Protocol error

I tried to connect to a new bbdd from laravel giving me the error that appears in the title
I leave below the configuration file of bbdd (database.php) and as we call it from the controller
'reporting' => [
'driver' => 'mysql',
'host' => env('DB_REPORTING_HOST', 'localhost'),
'port' => env('DB_REPORTING_PORT', '3306'),
'database' => env('DB_REPORTING_DATABASE', 'reporting'),
'username' => env('DB_REPORTING_USERNAME', 'paco'),
'password' => env('DB_REPORTING_PASSWORD', 'qwerty'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
],
and the call
$DB = DB::connection('reporting');
$DB->table('clients')->get();
fix it
'reporting' => [
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => '3306',
'database' =>'reporting',
'username' => 'paco',
'password' => 'qwerty',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
And in the controller
$dataL = DB::connection('reporting');//->select('select * from leyenda');
$dataL2 = $dataL->table('familias')->get()

How to change database name before authentication in laravel 5.1

I have multiple database. I want to change db name based on the url dynamically. How can I set particular db before authentication.
I want to change database from authentication to through out the application.
For ex.
If url is like lara.local.com/comapny1
then it will select database company1
If url is like lara.local.com/company2
then it will select database company2
Based on the selected database authentication will be done and selected database will be used for that user.
Make entry for second database in config/database.php
'company1' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'database1'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'company2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => 'database2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
By default your queries will use the mysql connection to change connection to 'company1'
DB::connection('company1')->select($query);
Additionally you can set database connection for Model
$someModel = new MyModel;
$someModel->setConnection('company1');
You can use the Request::is() to get URI from URL
if(Request::is('company1')){
//change database to company1
Config::set("database.connections.company1.database", 'company1');
}
elseif(Request::is('company2'){
Config::set("database.connections.company1.database", 'company2');
}
You can achieve this thing with below steps:
1 Define multiple connection into database.php for example:
'db1' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'db1_connection',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
2 Use connection based on conditional statement(You should move this code else)
//Check url that contains 'MYCODE'
if ('URL Contains' == 'MYCODE') {
DB::disconnect();
Config::set('database.default','db1');
DB::reconnect();
}
else {
DB::disconnect();
Config::set('database.default','db2');
DB::reconnect();
}
It should work for you, however I haven't tested it. Reference taken from answer by Marcin Nabialek
You can do this in AppServiceProvider's boot() like this
public function boot()
{
if($this->app['request']->getHost()=='test.com') {
Config::set('database.default','mysql');
}
else{
Config::set('database.default','mysql1');
}
}

Laravel 5 Changing Database name in controller

ers. I need to change the database name into a specific controller. I already changed the database.php into this
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'loal'),
'database' => env('DB_DATABASE', 'test1'),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
'sqlsrv2' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'local'),
'database' => env('DB_DATABASE', 'test2'),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
My main DB is test1 and I need to change it into test2 db name in here :
public function TransactionHistory(Request $request){
Config::set('database.default','sqlsrv2');
dd(DB::connection() );
}
But it only returns null and it is still reading test 1. Anyone?
One way is to change the connection by using DB::connection() method:
$connection = DB::connection('sqlsrv2'); //this will create a database connection using sqlsrv2 in your config.
Now, you can use $connection to run the queries, etc.
Reference :
If you are using Eluquent there is more elegant way Database Connection
Create enother config for second connection
`
'connections' => array(
# Our primary database connection
'mysql' => array(
'driver' => 'mysql',
'host' => 'host1',
'database' => 'database1',
'username' => 'user1',
'password' => 'pass1'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
# Our secondary database connection
'mysql2' => array(
'driver' => 'mysql',
'host' => 'host2',
'database' => 'database2',
'username' => 'user2',
'password' => 'pass2'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
);
`
Set connection in your Model:
`
namespace App;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model
{
/**
* The connection name for the model.
*
* #var string
*/
protected $connection = 'connection-name';
}

laravel Undefined variable: host

My problem is that I get the following error in MySqlConnector.php.
Undefined variable: host error
I am using two different connections for both read/write. I merged them in config/database.php file depending on environment that system uses. Here is the mysql connection codes.
<?php
switch($_SERVER['LARAVEL_ENV']){
case 'production':
$connections = array(
'mysql' => array(
'read' => array(
'host' => '127.0.0.1',
),
'write' => array(
'host' => 'hosturl',
),
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'mysql2' => array(
'read' => array(
'host' => '127.0.0.1',
),
'write' => array(
'host' => 'hosturl',
),
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
);
break;
case 'beta':
$connections = array(
'mysql' => array(
'read' => array(
'host' => '127.0.0.1',
),
'write' => array(
'host' => 'hosturl',
),
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'mysql2' => array(
'read' => array(
'host' => '127.0.0.1',
),
'write' => array(
'host' => 'hosturl',
),
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
);
break;
case 'development':
$connections = array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'mysql2' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'app_userdata',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
);
break;
}
return array(
'fetch' => PDO::FETCH_CLASS,
'default' => 'mysql',
'connections' => $connections,
'migrations' => 'migrations',
'redis' => array(
'cluster' => true,
'default' => array(
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
),
),
);
All I wanted to do is using a different host for reading and another one for writing. When I use one connection in localhost, I get no error. But in multiple connections, I get the error. What is the reason for the error?
Ok I found the problem. It was because of laravel version. I updated laravel 4.0 to 4.1 and problem was solved.
just for the record of your environment implementation.
You have a build in laravel the environmen detection and loading of specific conf files.
you can read here: http://laravel.com/docs/configuration#environment-configuration
but the general idea is to define your environments in the :
$env = $app->detectEnvironment(array(
'local' => array('your-local-machine-name'),
'production' => array('your--prod-machine-name'),
));
and than you go to config folder and create inside it a folder foreach environment you have.
create a production folder, and than each file you want to override simply copy paste it into the folder and edit the things you want to override.
implement this, and than check your errors back here again

laravel database connection returns undefined index error

I am developing a project using laravel 4 framework. In my database.php file I get the following error:
Undefined index: driver
And my connection is as following:
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
'mysql2' => array(
'read' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
)
);
I am also using environments in order to set different mysql connections. What is wrong with the code?
In my case it was because I deleted
'default' => 'mysql',
by mistake from app/config/database.php.
Moving the 'driver' key up a level should fix the issue.
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'driver' => 'mysql'
),
Most of the other params that are shared can me moved as well
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
),
'write' => array(
'host' => 'localhost',
),
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
This happens to me because I deleted
'default' =>env('DB_CONNECTION', 'mysql'),
From app/config/database.php. It's necesary have a default connection
Go to root directory .env
This values are taken first.
If you have multiple different connections (for example to multiple databases on the same host) and it doesn't make sense to set a default connection, you can specify a connection in the Model class.
<?php
namespace App\Http\Models;
use Illuminate\Database\Eloquent\Model;
class Character extends Model {
protected $connection = 'my_db_connection_name_here';
}
This would be on the of the connections defined in config/database.php.
I solved my problem by adding some permissions. My .env file was exists but wasn't readable. I just added 755 permission.

Categories