I'm currently working on a Laravel 5.5 project using Homestead on Windows 10. The situation is: my application needs to read data (read-only, no write) from an external database which is located on my physical machine. Because in production environment, this application has its own database and also needs to fetch data from a remote database. This external database is hosted on localhost using XAMPP.
I've searched the Internet and haven't got any answer that applies to my scenario. I'm totally confused now. I'm a student new to programming so can anyone please give me some tips on how should I configure to make this happen? Thank you so much!
[Update] About this app I'm working on: It is a web application which allows librarians and academic support staff to record details of each service, like which student came in asking about how to do referencing etc.
It will only run within my school, kind of like an internal app? Anyway, no one can access it outside the school, and it is for staff use only, not for students. This app has its own local database to store staff accounts and service details, and also as I mentioned above, it needs to fetch student information from the school database, for now during the development, I set up a database on my physical machine as the school database, and dump some dummy data in there.
Then I created a new model called Student using command line. When I tried to read data from the external database using php tinker, it showed error like this:
>>> App\Modles\Student::all()
PHP Fatal error: Class 'App/Modles/Student' not found in Psy Shell code on line 1
Below I included my Student.php, .env, and database.php files
App/Models/Student.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
protected $connection = 'mysql_campus';
}
.env:
......
//Local Database
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_DATABASE=w2_support
DB_USERNAME=homestead
DB_PASSWORD=secret
//External Database
DB_CONNECTION=mysql_campus
DB_EXT_HOST=10.0.2.2
DB_EXT_DATABASE=campus
DB_EXT_USERNAME=root
DB_EXT_PASSWORD=
BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
......
database.php:
......
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
//Local Database
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'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,
],
//External Database
'mysql_campus' => [
'driver' => 'mysql',
'host' => env('DB_EXT_HOST', '10.0.2.2'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_EXT_DATABASE', 'forge'),
'username' => env('DB_EXT_USERNAME', 'forge'),
'password' => env('DB_EXT_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
......