The most important bit is the setConnection() method called on Model before querying. Remember that using it this way you need all the connections defined in your config/database.php under connections.
class StudentsController extends Controller {
const DB_COUNT = 3;
private $students;
public function __construct(Students $students) {
$this->students = $students;
}
public function index(Request $request) {
for ($i=0; $i<self::DB_COUNT; $i++) { //or foreach through config('database.connections', [])
$this->students->setConnection('db_'.($i+1));
$students[] = $this->students->find(1)->students;
}
//what is "totalAVG" ???
$totalAvg = array_sum($students) / self::DB_COUNT;
}
}
Alternatively if we want to stick to particular connection names:
public function index(Request $request) {
foreach (config('database.connections', []) as $connName => $params)
$this->students->setConnection($connName);
$students[] = $this->students->find(1)->students;
}
//what is "totalAVG" ???
$totalAvg = !empty($students) ? array_sum($students) / count($students) : 0;
}