Laravel 非常灵活且高度可配置,你应该完全没有问题做这样的事情。对于数据库,例如,您可以创建两个连接:main
一个到主数据库表的固定连接,project
对于当前项目表,它应该如下所示:
'main' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
'project' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
你应该能够:
配置模型连接:
<?php
namespace App;
class Posts extends Model
{
protected $connection = 'project';
}
直接查询连接:
DB::connection('project')->table('users')->where('activated', true)->get();
在运行时配置数据库:
config([
'database.connections.project.database' => 'project1db',
'database.connections.project.user' => $user,
'database.connections.project.password' => $password,
]);
至于视图,你可以告诉 Laravel 在任何你需要的地方查找视图,只需执行以下操作:
View::addLocation('/path/to/project1/');