1

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,
    ],
    ......
Nay_said
  • 33
  • 1
  • 6

2 Answers2

7

You should be able to connect to 10.0.2.2 from your homestead to access the host machine. This is the standard loopback address for the host machine. It's sort of like how you would connect to 127.0.0.1 or localhost for a local database.

Peter
  • 1,615
  • 1
  • 9
  • 17
  • Thank you so much! And now I stuck with how to actually connect the external database. I've updated some information about this project I'm working on and along with the code, would you mind have a look? – Nay_said Nov 11 '18 at 10:18
  • You can try following this: https://stackoverflow.com/questions/31847054/how-to-use-multiple-database-in-laravel ... just enter your different connection details for the second connection. – Peter Nov 11 '18 at 18:09
  • 2
    Thanks you! Your response help me to connect to my Meilisearch service that was running on my host machine. God bless u good man – JosCarrillo Jun 10 '20 at 02:04
0

My laravel version is 5.1.

You can edit .env file and replace the DB_HOST value with the remote database host.

The other way is to edit config/database.php file.