What I want, is to access a separate database, based on the subdomain.
All other settings keep the same: username, password etc. Only database name needs to change.
I add This to database.php file
'subdomain' => [
'driver' => 'mysql',
'host' => '',
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
I also created Middleware
`$subdomain = $request->route()->account;
$customer = Customer::where( 'sub_domain', $subdomain )->first();
if( ! $customer ) {
// account not found, do something else
} else {
Config::set('database.connections.subdomain.host', 'testdomain');
}
return $next($request);`
this is in route.php
Route::group(['domain' => $domain, 'middleware' => 'subdomain'],function () {});
Use protected $connection = 'subdomain'; in Model
in .env I use default database for check customer database name
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=ispsoft
DB_USERNAME=root
DB_PASSWORD=
I got subdomain user database name. But not connected sudomain database And I did not get value from subdomain database
Try setting your model's $table, based on the subdomain. If it's merely a different mysql schema, you should be able to do something like this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Widget extends Model
{
/**
* The table associated with the model.
*
* #var string
*/
$subdomain = Config::get('database.connections.subdomain.host', 'testdomain');
protected $table = $subdomian.'.widgets';
}
Related
I am new in Laravel. I am facing a problem to migrate my users and password reset table.
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or
access violation: 1071 Specified key was too long; max key length is
767 bytes (SQL: a lter table users add unique
users_email_unique(email))
I have tried so many solution like (AppServiceProvider, database collation change, changing migration file ) but none of them give me a solution.
AppServiceProvider.php
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
}
Database.php
'mysql' => [
'driver' => 'mysql',
'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', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
],
After setting defaultStringLength in AppServiceProvider maybe re-run your project with php artisan serve solve your problem.
I'm developing a Multi Tenant with Laravel v5.7 and I'm successful in sending queue emails, since my models have the property 'connection' defined.
But when trying to send, for example, an email using the Jobs class, the same fails and informs that the table of model does not exist.
From what error recorded in the table 'failed_jobs', even with the property 'connection' defined, it appears that the Job nevertheless tries to connect to the main database and not to the specified database of the property.
Is there any way to specify in Job which database to use, since the same is informed in the model?
database.php
'connections' => [
'others' => ['...']
'TENANT001' => [
'driver' => 'mysql',
'database' => env('TENANT001_DATABASE', ''),
'host' => env('TENANT001_HOSTNAME', ''),
'port' => env('DB_PORT', '3306'),
'username' => env('TENANT001_USERNAME', 'forge'),
'password' => env('TENANT001_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
Sample Model
class Template extends Model
{
/**
* The database name used by the model.
*
* #var string
*/
protected $connection = 'TENANT001';
}
failed_jobs
PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'main_database.templates' doesn't exist in /www/samba/laravel.local/vendor/laravel/framework/src/Illuminate/Database/Connection.php:326
I guess you are trying to access second connection TENANT001 in Template Model.
class Template extends Model
{
/**
* The database name used by the model.
*
* #var string
*/
protected $connection = 'TENANT001';
protected $table='templates';
}
After that in your config/database.php
'connections' => [
'others' => ['...']
'TENANT001' => [
'driver' => 'mysql',
'database' => env('TENANT001_DATABASE', ''),
'host' => env('TENANT001_HOSTNAME', ''),
'port' => env('DB_PORT', '3306'),
'username' => env('TENANT001_USERNAME', 'forge'),
'password' => env('TENANT001_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
Or you can use connection in the queue.php
'database' => [
'connection' => 'TENANT001'
'driver' => 'mysql',
'table' => 'jobs',
'queue' => 'default',
'expire' => 60,
],
And clear the cache once you update .env files.
php artisan config:cache
And check you have define everything correctly in .env and table exists.
Thanks
How to connect two database in laravel-5 and how to get data from db.
I know two one thing for that
In config/database set like this.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'larashop'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'larashop2'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Using Query you can define a connection on the Query Builder:
$users = DB::connection('mysql2')->select('your query');
Using Eloquent
You can also define which connection to use in your Eloquent models as well!
<?php
class SomeModel extends Eloquent {
protected $connection = 'mysql2';
}
You can also define the connection at runtime via the setConnection method.
<?php
class SomeController extends BaseController {
public function someMethod()
{
$someModel = new SomeModel;
$someModel->setConnection('mysql2');
$something = $someModel->find(1);
return $something;
}
}
I have find the issue of connect 2 database in laravel.
If any body wants two work with 2 database then config as par above suggestion and remove database configuration from .env file which is located at root.
I have tried and I am working with 2 database.
Hello I've in config/database.php a prefix (mysql) like this:
'prefix' => 'myprefix_',
But I need, only for one model, to use a different prefix like:
protected $table = 'otherprefix_mytable';
In this way laravel looking for "myprefix_otherprefix_mytable".
Any help?
In your app/config/database.php make 2 different connections like
'connections' => array(
# first prefix
'mysql1' => array(
'driver' => 'mysql',
'host' => 'host1',
'database' => 'database1',
'username' => 'user1',
'password' => 'pass1'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'prefix1',
),
# second prefix
'mysql2' => array(
'driver' => 'mysql',
'host' => 'host1',
'database' => 'database1',
'username' => 'user1',
'password' => 'pass1'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'prefix2',
),
),
And then later in model you can use different connection
class SomeModel extends Eloquent {
protected $connection = 'mysql2';
}
For more help check this
Default table prefix can be overriding in laravel model as following example:
use Illuminate\Support\Facades\Config;
public function __construct(array $attributes = array()) {
$collection = Config::get('database');
$collection['connections']['mysql']['prefix'] = 'consultancy_';
Config::set('database',$collection);
parent::__construct($attributes);
}
I build an application for many tenants and each tenant has it's own database. The name of the database is the same as the tenants id.(the id exists out of five numbers). After authentication the user should be connected with his own database and redirected to his dashboard.
I tried to connect the user with his database with the following code, which I placed in Authenticate.php
if (!Auth::guest() && Auth::user()->tenant) {
$user = Auth::id();
Tenanti::driver('user')->asDefaultDatabase($user, 'users_{id}');
Config::set('database.connections.mysql.database', 'user_'.$user);
}
The if statement checks in the main database if the logged in user is a tenant(boolean).
The config/database.php contains the following code:
'tenants' => [
'user_1' => [
'driver' => 'mysql',
'host' => 'localhost', // for user with id=1
'database' => '86097', // for user with id=1
'username' => 'root', // for user with id=1
'password' => 'root', // for user with id=1
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
AppServiceProvider:
<?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'] = "user_{$entity->getKey()}";
return $template;
});
}
}
?>
I don't get an error, but the connection hasn't changed. The user database is empty and therefore I shouldn't see any data when I log in with user_id=1 . Thanks in advance for helping.
The configuration should be:
'tenants' => [
'driver' => 'mysql',
'host' => 'localhost', // for user with id=1
'database' => '86097', // for user with id=1
'username' => 'root', // for user with id=1
'password' => 'root', // for user with id=1
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Other than that please replace Tenanti::setupMultiDatabase() with Tenanti::connection() (we have deprecated the old method).
And change the following:
Tenanti::driver('user')->asDefaultConnection(Auth::user(), 'tenants_{id}');
Obviously if you want to use users_{id} you would then need to replace all tenants to users.