Laravel, Eloquent, insert data to custom database - php

I have created a custom db connection:
Config::set('database.connections.key', array(
'driver' => 'mysql',
'host' => $user[0]->db_ip,
'database' => $user[0]->db_name,
'username' => $user[0]->db_login,
'password' => $user[0]->db_passwd,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
));
It works because I can retrive data using ORM:
$d = Time::on('key')->find(1);
But I cannot insert any data.
I've tried normal insertion by ->save() and with ->create() functions.
Could anyone help me?

Related

Eloquent outside laravel change fetch mode to PDO::FETCH_ASSOC

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)

Laravel - query from database and save to another

Welcome! I try to find a solution for my problem but it's hard for fresh laravel user. I try to do some simple booking system. I have 2 tables: schedule, and bids. What I want to do is display schedule table and via "book" button save it to bids table. I know how to query schedule table but I don't know how to write controller to save same data to another table.
Regards and thank you for the help.
You can do it in in the same controller class by adding a new function inside it. Here is the sample code.
class YourController extends Controller
{
public function bookSchedule()
{
//here we are getting the data from the table
$s = Schedule::where('id','=',98)
->select('colomn_1','colomn_2')
->get()
->toArray();
// here we are storing it back to the bids table
Bids::insert($s);
}
Hope the answer has helped you.
first of all you should show some examples of what have you done,
also your database connection file
however you should connect the 2 databases in app/config/database.php like the following
<?php
return array(
'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 in the controller you can chose which database you are talking to
for example $users = DB::connection('mysql2')->select(...);
check the Laravel Documents

Connect to Different Database in October CMS

Connecting to Different Database in October CMS
Hello,
I have two databases first one was created by OctoberCMS during installation process and the other one which i had already. how do i connect to that other database, i tried using builder plug-in but i am confused how to do it.
I want to connect to the other database and fetch information from one table and display on my home page.
Please help me with this. I am newbie to this CMS.
Thank you.
In your config/database.php you will have something like this (assuming MySQL):
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => 3306,
'database' => 'database_name',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
Just add another one after it, changing the name to something else (mysql-legacy in my example below) and the relevant connection details:
'mysql-legacy' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => 3306,
'database' => 'database_name',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
After that, you can use DB; in your model or controller and then do things like this:
$query = DB::connection('mysql-legacy')->select('SELECT * FROM tablename');

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');
}
}

Is there a way to set db connection settings for all connection under same function in laravel 4?

Let say I have the following database settings in app/config/database.php
'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' => '',
),
),
From the official guide of laravel, there are a few ways to choose which connection settings to use. Either set it when I need to make the connection,
$users = DB::connection('mysql2')->select(...);
or set it in the model:
class SomeModel extends Eloquent {
protected $connection = 'mysql2';
}
In my case, I have duplicated databases with same schema for different users, so it is impossible to set it in the model. Is there a way to choose connection settings for all connection under the same function so that I can put it in the filter / baseController and set the connection on the run?
One option would be to update the 'database.default' key and reconnect:
Config::set('database.default', 'mysql2');
DB::reconnect();
Here is a third solution (the last one): http://fideloper.com/laravel-multiple-database-connections
So you can pass the connection name into the function or set it in the controller.
You can change the default connection to 'mysql2' just by doing this.
DB::setDefaultConnection('mysql2');

Categories