Database conflict when running two Laravel apps on same server
I have two Laravel apps that I'm trying to get to work together on a local XAMPP server. Specifically, I'm testing a reporting feature on application 1 (a retail-sales-tracking app) that requests some data from application 2 (an inventory-tracking app). In production the two run on different servers, but this is my local test machine so they're both running on the same server, and both databases are accessible from the same MySQL session.
I've written an API request handler in app 2 that can successfully handle the data request when I send it from Postman. However, when I try to call that API path from app 1, it fails because of a "table not found" error. Specifically, the error message in the log says "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'app1.settings' doesn't exist". There is no table called "settings" in App 1 -- but there is one in App 2, and the error is being thrown from App 2.
It looks like something somewhere is not getting reset to recognize the change from App 1 to App 2, so App 2 doesn't know to use "App2" as the database name instead of "App1". Is this correct? If so, how can I explicitly force App 2 to look at its own database in this situation?
By default databases settings are in .env file, it seems you messed some values between app1 and app2. Check carefully .env files of both apps (especially DB_NAME) and execute php artisan optimize:clear on both apps.
There is no DB_NAME defined in either app's .env file. There is a DB_DATABASE, and each env file has the correct database name there.
I noticed that one env file had the DB_HOST as 127.0.0.1 and the other had it as "localhost". I set both to be "localhost" and now the new function in App 1 won't run at all. Won't even acknowledge changes in its own code. No matter what I do, as soon as I try to call this new function, I get a blank screen. The rest of App 1 seems to work fine, but the new bit might as well not be there. I just changed the route:: call to call a function that doesn't exist, cleared browser cache, cleared Laravel cache, and tried calling it -- and still got just a blank screen. No error message of any kind, nothing in the console, just a blank screen.
Update: I figured out what I did wrong to get the blank screen and have fixed it. I still am getting the original error, in which App 2 tries to use App 1's database instead of its own.
It outputs database settings for app1. Do the same for app2 (go to app2 folder and execute commands). Compare the results. Are they the same or differ? Are they even correct for each app respectively?
Don't you have raw SQL queries in app2 that refer to app1 database by mistake? Maybe you copy-pasted a query and forgot to change db name. Do a text search for "app1_db_name" through app2 folder, in all files.
Config shows significant differences between app 1 and app 2: app 1 has connections to sqlite, mysql, mysql_old, pgsql, and sqlsrv. App 1 also has a redis entry for "cache" as well as the default one. App 2 has database connections for sqlite, mysql, and pgsql, and only a default redis connection. The database names used in both are correct.
There are a few raw queries in app 2, but none in the specific sequence that fires when the API call from App 1 comes in. App 2 doesn't mention app 1's database by name anywhere.
Is this something to do with the redis cache, perhaps?
If App 1 is running on Apache and App 2 is running on "php artisan serve", I don't get the error. It's clumsy to set up, but it looks like I can use that as a workaround. Thanks for the idea.
Gotta admit, though, I don't understand that at all. How can the php artisan server even see a database created in XAMPP's MySQL instance??
There are many possibilities, but most likely you left a forgotten wrong parameter value somewhere while copying configs or just experimenting. There are no miracles, for sure.
BTW you don't need Apache at all for local development, php artisan serve is enough. If you want to launch two apps at the same time use php artisan serve --port=8001 for the second and both apps will be available on http://127.0.0.1:8000 and http://127.0.0.1:8001 respectively.