How to connect to remote database from Laravel Homestead?











up vote
-1
down vote

favorite












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:



>>> AppModlesStudent::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 IlluminateDatabaseEloquentModel;

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,
],
......









share|improve this question
























  • Can you show what's on your .env file
    – Paul
    Nov 9 at 0:51






  • 1




    Welcome to SO. Is it related to programming? can you please show your code?
    – tod
    Nov 9 at 2:18










  • Code updated along with some additional information
    – Nay_said
    Nov 11 at 10:25















up vote
-1
down vote

favorite












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:



>>> AppModlesStudent::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 IlluminateDatabaseEloquentModel;

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,
],
......









share|improve this question
























  • Can you show what's on your .env file
    – Paul
    Nov 9 at 0:51






  • 1




    Welcome to SO. Is it related to programming? can you please show your code?
    – tod
    Nov 9 at 2:18










  • Code updated along with some additional information
    – Nay_said
    Nov 11 at 10:25













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











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:



>>> AppModlesStudent::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 IlluminateDatabaseEloquentModel;

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,
],
......









share|improve this question















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:



>>> AppModlesStudent::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 IlluminateDatabaseEloquentModel;

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,
],
......






php mysql laravel laravel-5.5 homestead






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 10:12

























asked Nov 9 at 0:20









Nay_said

33




33












  • Can you show what's on your .env file
    – Paul
    Nov 9 at 0:51






  • 1




    Welcome to SO. Is it related to programming? can you please show your code?
    – tod
    Nov 9 at 2:18










  • Code updated along with some additional information
    – Nay_said
    Nov 11 at 10:25


















  • Can you show what's on your .env file
    – Paul
    Nov 9 at 0:51






  • 1




    Welcome to SO. Is it related to programming? can you please show your code?
    – tod
    Nov 9 at 2:18










  • Code updated along with some additional information
    – Nay_said
    Nov 11 at 10:25
















Can you show what's on your .env file
– Paul
Nov 9 at 0:51




Can you show what's on your .env file
– Paul
Nov 9 at 0:51




1




1




Welcome to SO. Is it related to programming? can you please show your code?
– tod
Nov 9 at 2:18




Welcome to SO. Is it related to programming? can you please show your code?
– tod
Nov 9 at 2:18












Code updated along with some additional information
– Nay_said
Nov 11 at 10:25




Code updated along with some additional information
– Nay_said
Nov 11 at 10:25












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










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.






share|improve this answer























  • 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 at 10:18










  • You can try following this: stackoverflow.com/questions/31847054/… ... just enter your different connection details for the second connection.
    – Peter
    Nov 11 at 18:09




















up vote
0
down vote













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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53218091%2fhow-to-connect-to-remote-database-from-laravel-homestead%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    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.






    share|improve this answer























    • 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 at 10:18










    • You can try following this: stackoverflow.com/questions/31847054/… ... just enter your different connection details for the second connection.
      – Peter
      Nov 11 at 18:09

















    up vote
    1
    down vote



    accepted










    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.






    share|improve this answer























    • 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 at 10:18










    • You can try following this: stackoverflow.com/questions/31847054/… ... just enter your different connection details for the second connection.
      – Peter
      Nov 11 at 18:09















    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    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.






    share|improve this answer














    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 9 at 1:04

























    answered Nov 9 at 0:58









    Peter

    507213




    507213












    • 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 at 10:18










    • You can try following this: stackoverflow.com/questions/31847054/… ... just enter your different connection details for the second connection.
      – Peter
      Nov 11 at 18:09




















    • 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 at 10:18










    • You can try following this: stackoverflow.com/questions/31847054/… ... just enter your different connection details for the second connection.
      – Peter
      Nov 11 at 18:09


















    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 at 10:18




    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 at 10:18












    You can try following this: stackoverflow.com/questions/31847054/… ... just enter your different connection details for the second connection.
    – Peter
    Nov 11 at 18:09






    You can try following this: stackoverflow.com/questions/31847054/… ... just enter your different connection details for the second connection.
    – Peter
    Nov 11 at 18:09














    up vote
    0
    down vote













    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.






    share|improve this answer

























      up vote
      0
      down vote













      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.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 3:47









        PhilipLee

        1




        1






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53218091%2fhow-to-connect-to-remote-database-from-laravel-homestead%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini