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);
}
Related
In a multi-tenant Laravel app, each tenant has its own database connection. So after the user has selected his database, I want to authenticate the user using Auth::loginUsingId. Still, no matter what I do, I cannot change the Users Model's connection to another default.
If I set the connection in the model, it does connect to the specific database, but I want this to be done dynamically.
Is there a way to specify the connection dynamically that Laravel's auth should use for the authentication?
You could define another connection in your config/database.php file like this:
return array(
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database1',
'username' => 'user1',
'password' => 'pass1'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'second_db_connection' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database2',
'username' => 'user2',
'password' => 'pass2'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
)
And change the User model to be like this:
class User extends Model {
protected $connection = 'second_db_connection';
}
Before authenticating the user, change the database connection temporarily for the current request only using Conifg::set
$db = "database_name";
Config::set("database.connections.mysql.database", $db);
Hi i am trying to access database name by using config but unfortunately it's not working please help me how can i resolve that thanks.
Property model
public function editedBy()
{
return $this->belongsToMany('App\User',config('database.connections.web.database'), 'property_id', 'user_id')->withTimestamps();
}
app/config/database/
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
'web' => [
'driver' => 'mysql',
'host' => env('DB_WEB_HOST'),
'port' => env('DB_WEB_PORT'),
'database' => env('DB_WEB_DATABASE'),
'username' => env('DB_WEB_USERNAME'),
'password' => env('DB_WEB_PASSWORD'),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
You can try using to database name using env('DB_DATABASE'). Its displaying that you have change the default variables so, it may be like this in your case.
public function editedBy()
{
return $this->belongsToMany('App\User',env('DB_WEB_DATABASE'), 'property_id', 'user_id')->withTimestamps();
}
or if you really want to use config this is the example.
public function getDatabaseName()
{
$databaseName = Config::get('database.connections.'.Config::get('database.default'));
dd($databaseName['database']);
}
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.
I want to store multiple database connection in my current laravel db.
i need to connect these via dropdown and generate reports on the fly.
what is the best way to configure this
You can just create a table "connection(strings)" in your 1st db and create a model for that table.
After that you can do something like this:
$selected = Connection::query()->where('name', 'fromdropdown')->first();
$connection = $selected;
config(['database.connections.data' => array(
'driver' => 'sqlsrv',
'host' => $connection['Database_Server'],
'database' => $connection['Database_Name'],
'username' => $connection['Database_User'],
'password' => $connection['Database_Pass']
)]);
DB::setDefaultConnection('data');
You can set multiple database connections in the app/config/database.php file.
Example:
'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 just use the connection name in your query:
$users = DB::connection('mysql2')->select(...);
I'm trying to change the connection on the saving event. Here is what I have done so far:
I make and event on the saving method:
protected static function boot()
{
parent::boot();
static::saving(function($model)
{
$model->connection = 'test';
var_dump($model);
return $model;
});
}
And then I make a save on User.
$user = new User();
$user->save();
When that is done, it still uses the default connection instead of the test-connection I have set.
I have both a default and a test connection configured in app/config/database.php which you can see below: (OBS: The change is the prefix)
// ...
'connections' => array(
'default' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'test' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'test_',
),
),
// ...
The var_dump turns out with the following, where you can see that the $model->connection have been changed.
object(User)[159]
...
protected 'connection' => string 'default' (length=7)
...
But for some reasons it stills goes into the table users instead of test_users.
Does anyone have any idea how to make this work?
I figured out a way to do this by overriding Eloquent's save-method.
public function save(array $options = array())
{
// set draft connection
Config::set('database.default', 'test');
// do the actual save
parent::save($options);
// change back to original connection
Config::set('database.default', 'default');
}