Work with mysql database in Laravel 4.2 - laravel

I am new to Laravel used this https://github.com/msurguy/laravel-facebook-login link to work with facebook login in Laravel. I have setup all the things but i
got following errors when establishing database connection :
Type error: Argument 1 passed to
Illuminate\Redis\Database::__construct() must be of the type array,
null given, called in
C:\wamp64\www\laravel-facebook-login-master\vendor\laravel\framework\src\Illuminate\Redis\RedisServiceProvider.php
on line 23
I want to run simple mysql database. Below is my database.php code:
return array(
'fetch' => PDO::FETCH_CLASS,
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'facebookdb',
'username' => 'root',
'password' => '',
'port' => '3603',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
),
'migrations' => 'migrations',
);
Any suggestion/help will be highly appreciated.

You are missing the definition for your redis connection in that connections array, something like this will help:
'redis' => array(
'host' => env('REDIS_HOST', 127.0.0.1),
'port' => env('REDIS_PORT', 6379),
'password' => env('REDIS_PASSWORD', null),
'database' => env('REDIS_DATABASE', 0)
)
Make sure that you add the corresponding entries in your .env file if your redis connection settings differ from the default values above.

Related

Convert mongodb connection string to PHP

I have the following connection string generated from MongoDB:
mongodb+srv://admin:<password>#cluster0-n74zb.mongodb.net/test
I want to place the connection data in Laravel,
Here is what I have tried but it doesn't connect.
'mongodb' => array(
'driver' => 'mongodb',
'host' => 'cluster0-n74zb.mongodb.net/test',
'port' => 27017,
'username' => 'admin',
'password' => 'xxxxxx',
'database' => 'driving',
'options' => array(
'db' => 'admin' // sets the authentication database required by mongo 3
)
),

Dynamic Database Connection Using Laravel

I am working on a new application that must connect to several databases. The databases are dynamically created when a new company is registered in my application i.e each company have there on databases.
My question is how to establish the database connection using laravel,
If the company admin is login he needs to connect the corresponding db. How is it possible ???
I know how to establish multiple database connection if the databases are stable, like,
'mysql1' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge1'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
...
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge2'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
...
but if the databases are not stable how is it possible ????
If my question is not up to standard, please forgive me
You can add dynamically the database configuration like this.
if(NULL === config()->get('database.connections.company')){
config()->set('database.connections.company', [
'driver' => $companyDbDriver,
'host' => $companyDbHost,
'port' => $companyDbPort,
'database' => $companyDbName,
'username' => $companyDbUser,
'password' => $companyDbPassword,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false
]);
}
in the model; set the connection attribute to 'company'
/**
* Connection configuration ID.
*
* #var string
*/
protected $connection = 'company';
there are complication if you dont load the configuration of the database before calling the models using it, i'll leave that to you.
First you have to know where your database connections parameters are stored.
Anyway, wherever they are, you should create a new connection on the fly.
Supposing each User has it's own connection ...
<?php
use Illuminate\Support\Facades\Config;
$user = Users::find($id);
Config::set('database.connections.' . $user->username, array(
'driver' => 'mysql',
'host' => $user->db_host,
'database' => $user->db_name,
'username' => $user->db_user,
'password' => $user->db_pass,
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => '',
));
// And set the new connection to interested models
$myModel = new MyModel;
$myModel->setConnection($user->username);
$data = $myModel->where(...)->get();
Hope this help

Creating laravel multiple mysql connection from current database

I want to store multiple database connection in my current laravel db.
i need to connect these via dropdown and generate reports on the fly.
what is the best way to configure this
You can just create a table "connection(strings)" in your 1st db and create a model for that table.
After that you can do something like this:
$selected = Connection::query()->where('name', 'fromdropdown')->first();
$connection = $selected;
config(['database.connections.data' => array(
'driver' => 'sqlsrv',
'host' => $connection['Database_Server'],
'database' => $connection['Database_Name'],
'username' => $connection['Database_User'],
'password' => $connection['Database_Pass']
)]);
DB::setDefaultConnection('data');
You can set multiple database connections in the app/config/database.php file.
Example:
'default' => 'mysql',
'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' => '',
),
)
And then just use the connection name in your query:
$users = DB::connection('mysql2')->select(...);

Using Laravel, is there a way to check which database I'm connected to and change to another?

If I'm doing something simple like
Auth::user()->username
Is there a way I can change the database that I'm getting this information from?
I already have the other database connection information entered in the database.php file. Just not sure how to change it on the fly.
DB SETUP
'mysql2' => array(
'read' => array(
'host' => '192.168.1.1',
),
'write' => array(
'host' => '196.168.1.2'
),
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
In your AUTH driver specify the DB to use
app/config/auth.php
'driver' => 'database',
for diff table than users
'table' => 'administrators',
Create your own driver
http://laravel-recipes.com/recipes/115/using-your-own-authentication-driver

Multi Database Migrations - How to set the connection details

I'm creating a multi-tenant application with Laravel 4. At the moment I have two connections in my database.php
'connections' => array(
'app' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'homestead',
'username' => 'homestead',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'tenant' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
When connecting to a tenant database in the application I call something like this:
$tenant = Tenant::where('username', '=', $username)->first();
Config::set('database.connections.tenant.username', $tenant->db_username);
Config::set('database.connections.tenant.password', $tenant->db_password);
Config::set('database.connections.tenant.database', $tenant->db_database);
As you can see all my database names/connection details for tenants are set dynamically.
this is an issue when i'm trying to run a migration as I can't find a way to set the connection details.
The only way I can do this at the moment is to call my migration from a controller. Is their a better way?

Resources