Laravel laravel cross database relationships wherehas goes wrong - laravel

I have configured two database connections:
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', '####'),
'port' => env('DB_PORT', '####'),
'database' => env('DB_DATABASE', '####'),
'username' => env('DB_USERNAME', '####'),
'password' => env('DB_PASSWORD', '####'),
'charset' => 'utf8',
'prefix' => 'pref1_',
'prefix_indexes' => true,
],
'sqlsrv1' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST1', '####'),
'port' => env('DB_PORT1', '####'),
'database' => env('DB_DATABASE1', '####'),
'username' => env('DB_USERNAME1', '####'),
'password' => env('DB_PASSWORD1', '####'),
'charset' => 'utf8',
'prefix' => '',
],
With the normal user model and 2 custom models:
Synergy:
class Synergy extends Model
{
protected $connection = 'sqlsrv1';
protected $table = 'humres';
protected $primaryKey = 'res_id';
public $timestamps = false;
public function user(){
return $this->belongsTo('App\User', 'employee_id', 'res_id');
}
PushNotification:
class PushNotification extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
I want to check if the user has a permission for push notification and if another column is set on true.
I try it with this query:
$users = User::with('PushNotification')->with('Synergy')
->whereHas('PushNotification', function($q){
$q->where('type', 'news')->where('active', 1);
})
->whereHas('Synergy', function($q1){
$q1->where('freefield20', 1);
})
->get();
return $users;
It throws the following error:
SQLSTATE[42S02]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid object name 'pref1_humres'. (SQL: select * from [pref1_users] where exists (select * from [pref1_push_notifications] where [pref1_users].[id] = [pref1_push_notifications].[user_id] and [type] = news and [active] = 1) and (exists (select * from [pref1_humres] where [pref1_users].[employee_id] = [pref1_humres].[res_id] and [freefield20] = 1)))
Why does it use the prefix of sqlsrv while with sqlsrv1 no prefix is entered?
Any ideas how to build this query correctly?

Related

I am trying to access database name in model relationshipt by config using laravel 8

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

No suitable servers found: [connection refused calling ismaster on '127.0.0.1:27017']

No suitable servers found (serverSelectionTryOnce set): [connection refused calling ismaster on '127.0.0.1:27017']
//.env
DB_CONNECTION=mongodb
MONGO_DB_HOST=127.0.0.1
MONGO_DB_PORT=27017
MONGO_DB_DATABASE=dba
MONGO_DB_USERNAME=
MONGO_DB_PASSWORD=
//database.php
'mongodb' => [
'driver' => 'mongodb',
'host' => env('MONGO_DB_HOST', '127.0.0.1'),
'port' => env('MONGO_DB_PORT', 27017),
'database' => env('MONGO_DB_DATABASE'),
'username' => env('MONGO_DB_USERNAME'),
'password' => env('MONGO_DB_PASSWORD'),
'options' => [
'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'),
'ssl' => false
]
],
//Model
class Question extends Eloquent
{
protected $connection = 'mongodb';
protected $collection = 'questions';
protected $fillable = [
'title', 'content','location'
];
}
//php_info
php_info
I'm using XAMPP, LARAVEL 5.8, PHP 7.2 and MongoDB 1.7.3
Any solution?

Two database connect in laravel

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.

Override table prefix for a model

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

Change connection on saving-event

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

Resources