Unable to connect with database in laravel - laravel

I am trying to connect with database on runtime in which i am setting connection settings from session like following. first connection (weserach) was successful but the connection from session variable(wesearch_barcode) wasn't.
this is database.php
'wesearch' => [
'driver' => 'mysql',
'host' => 'waev.in',
'port' => env('DB_PORT', '3306'),
'database' => 'wesearch_waev_user',
'username' => '******',
'password' => '******',
'charset' => 'latin1',
'collation' => 'latin1_swedish_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'wesearch_barcodedb' => [
'driver' => 'mysql',
'host' => 'waev.in',
'port' => env('DB_PORT', '3306'),
'database' => $_SESSION['dbname'],
'username' => $_SESSION['username'],
'password' => $_SESSION['password'],
'charset' => 'latin1',
'collation' => 'latin1_swedish_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
this is my controller:
namespace App\Http\Controllers;
session_start();
use Illuminate\Http\Request;
use Illuminate\Database\DatabaseManager;
use Illuminate\Support\Facades\DB;
use App\Http\Requests;
use App\central_db;
use App\barcodedb;
class check_subsController extends Controller
{
public function fetch(Request $request){
if($db=\App\central_db::select(['db_name', 'password', 'first_name', 'last_name'])->where('device_srno','=',$request->srno)->get()){
foreach ($db as $record) {
$_SESSION['dbname'] = $record->db_name;
$_SESSION['username'] = $record->last_name;
$_SESSION['password'] = $record->first_name;
}
if($users=\App\barcodedb::where('park','=','1')->get()){
return $users;
}
}
}
}
This is my central_db model:
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class central_db extends Model
{
protected $connection ='wesearch';
protected $table = 'usr_waev';
}
And this is another one model:
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class barcodedb extends Model
{
protected $connection ='wesearch_barcodedb';
protected $table = 'user';
}
And the error is something like this
"PDOException in Connector.php line 119:
SQLSTATE[HY000] [1045] Access denied for user ''#'139.59.29.54' (using password: NO)"

Related

Laravel laravel cross database relationships wherehas goes wrong

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?

i'm connect multiple Database and i use Laravel Excel I can'n export. This is error "Database [mysql] not configured."

i'm connect multiple Database and i use Laravel Excel I can'n export. This is error "Database [mysql] not configured."
i'm use laravel 6.0 and phpmyadmin version 4.4.15.10
controller
public function exportExcel()
{
return Excel::download(new merakiExport, 'MerakiBluetooth.xlsx');
}
app/Exports
namespace App\Exports;
use App\MerakiBluetoothTemp;
use Maatwebsite\Excel\Concerns\FromCollection;
class merakiExport implements FromCollection
{
/**
* #return \Illuminate\Support\Collection
*/
public function collection()
{
return MerakiBluetoothTemp::all();
}
}
config/database
'meraki' => [
'driver' => 'mysql',
'host' => env('DB_HOST5', ''),
'port' => env('DB_PORT5', ''),
'database' => env('DB_DATABASE5', 'forge'),
'username' => env('DB_USERNAME5', 'forge'),
'password' => env('DB_PASSWORD5', ''),
'unix_socket' => env('DB_SOCKET5', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
env
DB_HOST5=sql.laravel.com
DB_PORT5=3306
DB_DATABASE5=time_sheet
DB_USERNAME5=*****
DB_PASSWORD5=*****
I succeeded too.
app/Exports
namespace App\Exports;
use App\MerakiBluetoothTemp;
use Maatwebsite\Excel\Concerns\FromCollection;
use App\Http\Controllers\Controller;
class merakiExport extends Controller implements FromCollection
{
/**
* #return \Illuminate\Support\Collection
*/
public function collection()
{
return MerakiBluetoothTemp::all();
}
}

How to use Multiple Databases based on subdomein in Laravel

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

Laravel with multiple database

I have laravel project used for apis(Passport). It uses multiple databases one database stores user information and the database name for that user. I created a BaseController, all the other controllers extends from this controller. But I am not able to fetch Auth::id(); from the BaseController. How can I get the Auth::id in the constructor?
class BaseController extends Controller
{
protected $companySchema;
public function __construct() {
$user = \App\User::where('user_id', Auth::id())->first();
$company = $user->company;
$this->companySchema = $company->cmp_Schema;
}
}
After laravel 5.3.4 you can't use Auth::user() in the constructor because the middleware isn't run yet.
class BaseController extends Controller {
public function __construct() {
$this->middleware(function ($request, $next) {
$this->companySchema = Auth::user()->company->cmp_Schema;
return $next($request);
});
}
}
try this code and tell me if it works
You cannot use Auth in constructor, because middleware is not run yet. its from 5.3
check this link : https://laravel.com/docs/5.3/upgrade#5.3-session-in-constructors
First you should setup a connection in config/database.php:
'my_new_connection' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => $your_db_name,
'username' => $your_db_username,
'password' => $your_db_password,
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
Then you can change default database connection dynamically by this:
Config::set('database.default', 'my_new_connection');
You should first call
$user = Auth::user();
which is returning you the current user model associated, which is normally User. Then following should work:
$user->id

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

Resources