Laravel - query from database and save to another - php

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

Related

How to pass the value of a variable between 2 different sites

So. There is 2 different sites.
site_1 and site_2
Site_1:
PHP 7.3
Laravel 6.0
MYSQL
Contains corporate portal with helpdesk,news and so.
Site_2:
PHP 7.2
Laravel 5.6
MYSQL
Contains Videoportal(yeah like youtube:)) with users and webcams from construction sites.
On site_1 i have a class User with some properties (i.e. user id,department,and so) contains in db.
On site_2 i have different DB with user_id and cams .I want to select only webcams that belongs to user from site_1.
How can i pass value of User_id from site_1 to site_2 to select cams only for exact user?
In database.phpm define second mysql connection as
<?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' => '',
),
),
);
Then use
$users = DB::connection('mysql2')->select('select * from users');
Hint
https://laravel.com/docs/5.7/database#using-multiple-database-connections
Are they using the same database or are the databases on those apps synced? If not, then this is a bad practice. Well as for passing values from one Laravel app to another, just create endpoints wherein it will send/accept values.
Read the docs especially on the Controllers and Routing and then study on how you will implement it using your own logic.

Laravel application trying to authenticate user using another database

I have two applications both developed in Laravel.I have logged into the first application and doing coding. But when I try to log into the second application it gives an error of the missing 'users' table(the table in the second database I am trying to log into now which is not in the first logged in an application).
Seems like the second application is trying to authenticate the user using the first database which is not correct
Can anyone assist me with this
Dear Laroja as i get you want to authenticate with another application which have separate database then you have to define two database auth and try to build the logic if connect to first application then it uses the first database and if you want to connect with another application it will connect to other database.
Please have a sapmle code for connect two databse
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'qmops_live_server',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
# Secondary database connection
'mysql2' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => env('DB_PORT', '3306'),
'database' => 'XXX',
'username' => 'XXX',
'password' => 'QXXXX',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
please use the above code in app/config/database.php hope my solution will help you Thanks.

Seeding tenant database in Laravel 5

I am working on building a system using a main database and multiple tenant databases in laravel 5. I have a database migration and seed for the main database no problem, using php artisan migrate:refresh --seed.
Tenants then get their own database upon registration. I need to run a migration and seed on the tenants database.
The tenant migration files are stored in a separate folder. The migration runs (unfortunately on the main database) with the following command
\Artisan::call('migrate', [
'--path' => "database/migrations_system"
);
However I need the migration to occur on the tenant database, say DB_1.
I read the following should work
\Artisan::call('migrate', [
'--path' => "database/migrations_system",
'--database' => 'db_1'
]);
However I end up with an error
InvalidArgumentException in DatabaseManager.php line 238:
Database [db_1] not configured.
Stuck.... How can I specify the migration to run on a specific database?
UPDATE:
I have found that changing config/database.php and adding db_1 gets me past this error....
'db_1' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => 'db_1',
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'port' => '3306'
],
However this connection is unknown and needs to be done on the fly, which I am also stuck on how to accomplish....
UPDATE Again and working solution.... Modify the config on the fly...
$connections = \Config::get('database.connections');
$tenant_database = 'db_1'; //assign from your main database
$tenant_connection = [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => $tenant_database ,
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'port' => '3306'
];
$connections['tenant'] = $tenant_connection;
\Config::set('database.connections', $connections);
\Artisan::call('migrate', [
'--path' => "database/migrations_system",
'--database' => 'tenant'
]);
So it looks like I have answered my own question, however maybe some can comment on this procedure.
I'll put my own solution down....
1) Create the database name
For example I name the new database db_1 where 1 represents the ID of the system in the main database. I have considered storing a unique scrambled name in the main database. In the end that seemed like overkill.
2) Create the connection - for this I use the exact same name as the new database.
public function createConnection()
{
$connections = \Config::get('database.connections');
if(!isset($connections[$this->getSystemName()]))
{
$tenant_connection = [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => $this->getSystemName(),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'port' => '3306'
];
$connections[$this->getSystemName()] = $tenant_connection;
\Config::set('database.connections', $connections);
//dd(\Config::get('database.connections'));
}
}
3) Create the database, which you do using your main connection
$sql = "CREATE DATABASE " . $this->tenant_name;
DB::connection('main_db')->statement($sql);
4)run the migration
$r = \Artisan::call('migrate', [
'--path' => "database/migrations_tenant",
'--database' => $this->tenant_name
]);
5) finally you can seed test data or load some default data. Access the connection like
DB::connection($this->system_name)->table($table)->insert($csv);

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

Laravel, Eloquent, insert data to custom database

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?

Categories