laravel whereHas with multiple database connections
-
I have small issue with Laravel application , currently I am using two database connection and I want to change the connection in whereHas method.
here is my code .
//Relation Ship Methods public function technician(){ return $this->setConnection('common_database')->belongsTo('App\User', 'technician_id'); } public function commitee(){ return $this->setConnection('common_database')->belongsTo('App\User', 'commitee_validator_id'); }
//controller functions $practices->whereHas(‘technician’, function($q) use($search) { $q->where(‘first_name’, 'like', '%' . $search . '%'); });
-
In whereHas you cannot change the database connection, you have to change connection before where has like this
//get users ids with where $user_ids = User::where('first_name', 'like', '%' . $search . '%')->pluck('id');
In User modal you must specify the database connection before above where statement
protected $connection = 'common_database';
Then where in function to find the practices with user ids
$practices->whereIn('technician_id', $user_ids);
-
@root thanks root . let me check it