I have a very specific situation regarding databases in Laravel.
We have two DIFFERENT servers, one with SQL SERVER and one MySQL.
The regular join (belongsTo, hasMany) between two different connections and different servers is working flawlessly and I can get all the data that I want.
The problem occurs when I want to add WHERE parameters to the relationship - Laravel will add subquery "and exists" into the query - which will, of course, fail because we have two different servers.
Both models have correction table and connection specified in model properties and as I said regular belongsTo and hasMany is returning the correct results from both servers. Only WHERE conditions are failing the query.
What are the ways to solve this problem and how do you usually deal with this?
Much appreciated!
I'm unsure if that's solving your problem but you could try the package laravel-cross-database-subqueries by hoyvoy.
Just extend your models with Hoyvoy\CrossDatabase\Eloquent\Model.
I had a similar requirement a while ago. The solution I settled on was to replicate the data via a cron job from the MSSQL server into my MYSQL server, but this was largely because the MSSQL server was extremely slow in comparison to the MYSQL server. This allowed me to query correctly.
Alternatively, if you don't want to replicate the data and can get good response times from the MSSQL server, then you may want to look at "temporary tables". If you query the MSSQL server first and then create a temporary table in the MYSQL server with this data to query against, this may suit as a work around.
If none of the above suit, you will need to query the two separately and use PHP to manipulate the data because you can't query MYSQL and MSSQL in the same query.
I see 2 options, the first one is duplicate the database, but you might run behind so perhapse not the best method.
The second option create a seperate query for the relationship filter and use the result of that query to fetch the data
First create connection on config/database.php
return [
'default' => 'first_db_connections',
'connections' => [
'first_db_connections' => [
'driver' => 'sqlsrv',
'host' => 'localhost',
'database' => 'database1',
'username' => 'user1',
'password' => 'pass1'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
'second_db_connection' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database2',
'username' => 'user2',
'password' => 'pass2'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
],
],
So your models use the normal default connection, then create model second connection to your models
class myModel extends Model {
protected $connection= 'second_db_connection';
protected $table = 'myTable';
public function post() {
return $this->belongsTo('App\Post');
}
}
Related
I have two databases for my Laravel 8 project: DB_COMMON and DB_SYSTEM. The point is, that I want to separate sensitive data to avoid deleting, so I'll use one db_user (with all privileges) for DB_COMMON and another (without deleting or updating permissions) for DB_SYSTEM.It will be something like additional security layer.
And there are table/(-s), for exapmle, rbac_role. I want to store two roles as "system" (S_ADMIN and CUSTOMER) in DB_SYSTEM and allow user to create new roles in same table name (rbac_role), but in DB_COMMON. And usually I want to work with that roles in one place (one model).
Are there any way to do it?
Or, maybe, I can "push" this two roles in model? I mean merge rows from database table with my (maybe even hardcoded) rows in model?
First of all you have to create two separate connection in your config/database.php file.
mysql connection is default if you are using mysql database.
Copy that params and create new connection:
'connection2' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
...
],
Then you can specify any model to run on specific connection:
class RoleModel extends Model {
protected $connection = 'connection2';
...
}
When you call RoleModel it will connect to second database.
I don't fully get what you want, But if you want to select from rows from two database you can set connection dynamically and select rows from any table.
$first_rows = RoleModel::setConnection('mysql')->select('id', 'role')->get();
$second_rows = RoleModel::setConnection('connection2')->select('id', 'role')->get();
$combined_rows = $first_rows->merge($second_rows);
If you want to create new row in second connection depending whether same record exists in first connection:
if(RoleModel::setConnection('mysql')->where('role', 'somerole')->exists()){
RoleModel::setConnection('connection2')->create([
'role' => 'somerole'
]);
}
Hope this answer helps you
I have strange issue. I have two laravel projects on the same server(local server) and I want to retrieve data from one project into another. Each project has its own database and credentials. Now about problem: When I am sending GET/POST request using curl(), another project(destination project) uses credentials of the project I am sending request from. So select statement does not work due credentials and table names mismatch. When I dumped uri and pasted it into web browser it works as expected. Now question: Why second project using database credentials(or maybe other configs too) of the project I am sending request from? What is this shared session or something like this?
I am having a little trouble getting your question, but I am assuming you are using credentials from one project and are somehow getting a return in your other project which has different credentials. On top of that, you also have different databases. How could that be? Well to put it short, it shouldn't be. Can it be done? Yes, but you would have to do it on purpose. There is no shared session. So I would check your database connections in both projects to see if there is some overlap. If there is, check those out as your issue may be in there. If that is not the case, then check your curl request and see if you are passing the correct project credentials and are therefore getting the right result. Other than that, I would need more information to help you out.
Thanks!
And oh, are you using Laravel Passport? If not, what are you using/doing?
Did you solve this problem ?
I faced the same situation and found solution about this.
Solution is to change the .env file of one project as follows.
change .env as follows.
DB_X_HOST="localhost"
DB_X_DATABASE="other_project"
DB_X_USERNAME="homestead"
DB_X_PASSWORD="secret"
DB_X_PORT="3306"
change config\database.php as follows.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_X_HOST', '127.0.0.1'),
'port' => env('DB_X_PORT', '3306'),
'database' => env('DB_X_DATABASE', 'forge'),
'username' => env('DB_X_USERNAME', 'forge'),
'password' => env('DB_X_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
]
It worked fine for me.
I have MongoDB Active Records (models) and I'm wondering if it's possible to use Redis to automatically set/get/delete the models from Redis's storage.
For example, if I was to run:
MyModel::find()->where(["id" => 1])->one();
is there a way to make Redis store the result and return it the next time I run that same code?
Also, if I was to update the model with id = 1, I'd expect Redis to invalidate the cache.
Is all that possible?
It doesn't matter which DB to use. It is about how to implement them. Yii have those two components to set in the config file:
db: a database connection to be used where needed like an Active Record class to represent a model or Query Builders or ...
cache: designed to cache things from HTML pages and HTTP requests to database queries related data.
The good thing about MongoDB and Redis is that both can be used as a database connection or as a cache component. You may have those configs for example:
'components' => [
'db' => [
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://developer:password#localhost:27017/mydatabase',
],
'cache' => [
'class' => 'yii\redis\Cache',
'redis' => [
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
]
],
],
Here, while MongoDB is set as your default database, Redis is only used as a cache component and because all cache components have the same base class yii\caching\Cache they only support those APIs. Which should be fine if you are using it for caching proposes only.
Check the Yii2 Caching Guide to see all what you can do with a cache component. A quick example of what you are trying to do may be seen within #Blizz answer here where he did set an SQL query as a dependency to check if cached data should be used or invalidated instead.
In case you need to use the Redis database for more than just caching then you may have those configs instead:
'components' => [
'mongodb' => [
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://developer:password#localhost:27017/mydatabase',
],
'redis' => [
'class' => 'yii\redis\Connection',
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
],
'cache' => [
'class' => 'yii\redis\Cache',
'redis' => 'redis' // id of the connection application component
],
],
Here we defined 2 databases and selected one of them to be also used as a cache component. It should work the exact same way except that you can also use the Redis database inside your app as a Redis ActiveRecord or a Redis ActiveQuery class. You will just need to set which DB to be used inside each model class as it is done in this example.
I am trying to build and application using Laravel 5. It is supposed to be a multi tenant database architecture using multiple databases. My employer requires this for security purposes.
I have tried manually managing the main DB migrations and the Tenant migrations but failed. So I decided to take the help of a Laravel specific package which is supposedly what I require.
Tenanti provides a way to have my purpose solved but the problem is that me being a novice developer, am not able to fully understand how to use it in my application.
I have installed it correctly I believe doing:
composer require "orchestra/tenanti=~3.0"
Adding these providers and aliases in the config app file:
'providers' => [
// ...
Orchestra\Tenanti\TenantiServiceProvider::class,
Orchestra\Tenanti\CommandServiceProvider::class,
],
'aliases' => [
'Tenanti' => Orchestra\Support\Facades\Tenanti::class,
],
Finally publishing the config and tweaking it according to the documentation for multiple databases:
php artisan vendor:publish
return [
'drivers' => [
'user' => [
'model' => App\User::class,
'migration' => 'tenant_migrations',
'path' => database_path('tenanti/user'),
],
],
];
At this point I am still blurry what to do next?
My doubts are as follows:
Where will the migration files be generated and stored? I mean there are two kinds of databases in my application obviously. One set of files is for the main DB which will store all the tenant information and the other files will be for the tenant DB. So how and where will these be stored?
I see the word 'driver' a lot in the documentation but I am not sure what driver is exactly.
How will I handle the authentication for the application? I mean whenever a tenant logs in, I will have to make sure the connection to the database changes dynamically. How will I accomplish this?
I tried to go through the repository of the package itself and make sense of the code inside but in vain. I am not very good when it comes to design patters like facades, command bus, service provider and so on, which is why I am not able to understand the flow of the package or make sense of it.
I tried to run some of the artisan commands which come with the package like:
php artisan tenanti:install {driver}
php artisan tenanti:make {driver} {name}
But I am getting an error like so:
[InvalidArgumentException] Database connection
[tenants] is not available.
Where can I find the resources to understand how to proceed with this?
+1 to #morphatic answer, it quiet accurate on most of the stuff.
Migration
One set of files is for the main DB which will store all the tenant information and the other files will be for the tenant DB. So how and where will these be stored?
For your main database you should be able to use the default database/migration and utilize php artisan make:migration and php artisan migrate.
Tenanti however will use the migration path set under the "driver" configuration. e.g:
'path' => database_path('tenanti/user'),
In this case the migration will be created/migrated from database/tenanti/user (you can choose other folder and it will use that folder). Once you set this up you can create new migration file for the user tenant via php artisan tenanti:make user create_blogs_table (as an example) and run migration via php artisan tenanti:migrate user (see the similarity between Laravel migration command and Tenanti?).
Driver
Driver is just the grouping of a tenant, you maybe grouping it by users, companies, or team etc. And there is possibility that you may require more than one type of group per project, otherwise most of the time you only be using single "group" or "driver".
Authentication or Accessing DB
How will I handle the authentication for the application? I mean whenever a tenant logs in, I will have to make sure the connection to the database changes dynamically. How will I accomplish this?
First of all, you need to consider how you're planning to distinguish each tenant. Most of the time I would see people tend to opt for subdomain. So in this case you need to check if the subdomain belongs to any of the user (by querying the main database) using a middleware and then connect to the database that belongs to the user.
Tenanti doesn't manage that part of the process, because everyone has different style on that aspect, but we do provide a code to dynamically connect to your database tenant from a base database configuration.
Let say you have the following config:
<?php
return [
'fetch' => PDO::FETCH_CLASS,
'default' => 'primary',
'connections' => [
'primary' => [
//
],
'tenants' => [
'driver' => 'mysql',
'host' => 'dbhost', // for user with id=1
'username' => 'dbusername', // for user with id=1
'password' => 'dbpassword', // for user with id=1
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
],
'migrations' => 'migrations',
'redis' => [ ... ],
];
You can follow the step available in https://github.com/orchestral/tenanti#multi-database-connection-setup and add the following code.
<?php namespace App\Providers;
use Orchestra\Support\Facades\Tenanti;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Tenanti::setupMultiDatabase('tenants', function (User $entity, array $template) {
$template['database'] = "tenant_{$entity->getKey()}";
return $template;
});
}
}
This would ensure that you be using tenant_1 database for user=1, tenant_2 database for user=2 and so on.
So how does Tenanti detect which user if active?
This is where you need to add logic in your middleware.
$user = App\User::whereSubdomain($request->route()->parameter('tenant'))->first();
Tenanti::driver('user')->asDefaultDatabase($user, 'tenants_{id}');
I've never used this package, but using the code you submitted above here's what I think is probably close to the right solution. You will probably still need to play with some of these values to get them correct:
Migration Paths
Since you're using the multi-database configuration, I believe you should be able to keep your migrations in the normal location, i.e. database/migrations. Tenanti will then create an exact replica of the database for each tenant in a different database. However, when you run php artisan tenanti:install user it might actually create a folder under database/ that indicates where you should put your migrations.
What is a "driver"?
The driver describes whether Tenanti will use a single or multiple databases, what models to use for determining different tenants, and where to store migrations. It is what you identified in the Tenanti config file you used above.
Database Connection Selection
You need to update config/database.php as follows. In a normal Laravel app, you would have the DB connection setup as follows:
<?php
return [
'fetch' => PDO::FETCH_CLASS,
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'sqlite' => [ ...DB connection info... ],
'mysql' => [ ...DB connection info... ],
'pgsql' => [ ...DB connection info... ],
'sqlsrv' => [ ...DB connection info... ],
],
'migrations' => 'migrations',
'redis' => [ ... ],
];
However, in the case of Tenanti multi-database setup, you need to add in different connection info for each tenant's database. To do this you would add a new level to your database.php config file (this example assumes you're using mysql, but you could use any DB, or even different database engines for different tenants):
<?php
return [
'fetch' => PDO::FETCH_CLASS,
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'tenants' => [
'user_1' => [
'driver' => 'mysql',
'host' => 'dbhost', // for user with id=1
'database' => 'dbname', // for user with id=1
'username' => 'dbusername', // for user with id=1
'password' => 'dbpassword', // for user with id=1
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'user_2' => [
'driver' => 'mysql',
'host' => 'dbhost', // for user with id=2
'database' => 'dbname', // for user with id=2
'username' => 'dbusername', // for user with id=2
'password' => 'dbpassword', // for user with id=2
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
],
'migrations' => 'migrations',
'redis' => [ ... ],
];
As you can see, each tenant has its own database instance that can be located on a different host and have a different username/password. Tenanti needs to be told how to figure out which database to use. This is what the documentation on Database Connection Resolver describes. In their example, they've named their tenant databases using acme_{$user->id} whereas in my example above I used user_{$user->id}.
Like I said, I've never actually set this up myself, but these are my best guesses based on the docs, and having used other packages by this same developer. Hope this helps!
I use Laravel 5.1 and a mysql server.
I have a database with table in utf8 unicode, and my databse configuration file is like this :
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'false',
),
But when I use queries from eloquent, my texts which have accents are broken. It's not a problem from blade because I can write accent in the views correctly, and if I use PDO directly my text is nice.
How can I solve my problem with my queries from eloquent ?
I found my problem, I hope it will help others :
My Mysql databases are encoded in latin1 by default. I was sure that my databases were in utf8 so I configured as that in the configuration file on my laravel project.