Prerequisites
In my local environment I am working with multiple tenants and Redis (Auth required).
To serve the project I am using Valet.
For this case I am addressing these two connections:
- basic_foo (is defined in my .env)
- tenant_foo (is the one to change to during a request)
Until now I successfully changed the connections like so:
config()->set('database.connections.mysql',
array_merge(
config()->get('database.connections.mysql') ,
['database' => 'tenant_foo']
);
Problem
However, now I am seeing an issue with the query builder, keeping or falling back to the basic connection.
I get the expected connection results of tenant_foo (same for Redis) when I run
dd(config()->get('database.connections.mysql'));
I get the wrong but apparently active results of basic_foo when I run
dd(\DB::connection()); // returns Illuminate\Database\MySqlConnection
So all in all the app will return this Illuminate\Database\QueryException
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'basic_foo.table_bar' doesn't exist...
where it should search for
'tenant_foo.table_bar'
Things that did not solve the problem yet
restarting Redis
reinstalling Redis
php artisan config:cache
php artisan cache:clear
php artisan route:clear
php artisan view:clear
php artisan optimize
composer dump-autoload
Simply changing the database name to tenant_foo like below is not enough, as the config array remains the same of basic_foo.
\DB::connection()->setDatabaseName('tenant_foo');
Thoughts
I want to change the config-array the of \DB::connection(), but I don't know another way than the config->set().
I installed Telescope could this affect the db connection?
Any other ideas?
To dynamically change database name you should use:
DB::disconnect();
Config::set('database.mysql.database', 'tenant_foo');
DB::reconnect();
This worked for me:
\DB::disconnect('mysql');
Config::set('database.connections.mysql.database', 'tenant_foo');
\DB::reconnect('mysql');
Related
Now this is a really weird one.
My .env file is fine. When I run
php artisan config:clear
everything works. But when I run
php artisan config:cache
my database connection stops working:
SQLSTATE[HY000] [1045] Access denied for user 'foobar'#'xxx.xxx.xxx.xxx'
So again: My settings are correct. But when they are cached, they stop working. How could this possibly be?! Laravel seems to use some kind of old settings when using the cache, but I have no idea where these are coming from?
What I already tried:
php artisan optimize:clear
rm -rf bootstrap/cache/*.php
I ran into this issue while trying to setup a Laravel forge deployment script for zero downtime.
I hard coded the path to the ssl files for my database connection in /config/database.php. Changed that to use the values of .env, now it's working.
I'm facing a problem to save data by eloquent right after rename the database column name.
The column name was changed inside of the migration. The migration was carried out and the name in the database now is ok.
In the model, the column name was also changed to the new name.
But for some reason, when I try to insert a new record into the database, Laravel is complaining about the column name, it's trying to use the old name instead of the new name.
I'm currently using PHP 7 with Laravel 5.8 and Postgres as database. My environment is running on docker.
I already tried to clear the cache but it does not solved my issue.
Composer dump-autoload was also carried out but nothing changed.
This is the error:
Illuminate \ Database \ QueryException (42703)
SQLSTATE[42703]: Undefined column: 7 ERROR: column "name" does not exist LINE 1
Is import to say that i'm using Laravel Backpack.
It is possible due to cache issue sometimes so it is preferable to run following five commands after you do some changes to your migration or env file and Laravel isnt working as expected. Here is full explanation:
https://www.youtube.com/watch?v=Q1ynDMC8UGg
php artisan config:clear
php artisan cache:clear
composer dump-autoload
php artisan view:clear
php artisan route:clear
Please try these and see if it works after.
Please update your model and please also try with fluent query for testing like:
DB::table('users')->insert(array('name'=>'xyz'));
I have created many tables use migration file, but I have dropped some table via the mysql command line tool, then I find the migration status about the tables I have dropped is still ran, so how can i fix it?
When I use migrate command, it always says nothing to migrate, but I have no table in my database.
I have tried to delete all the logs and stuff under the storage path.
I have also tried to delete the migration table on mysql or change .env file to use a new database, but both cannot work. Can anyone help me?
I use laravel 5.1.
`Run migrate:rollback (to undo the last migration)
If migrate:rollback doesn't work ; do it manually:
step 1 : delete the migration file
step 2 : composer dump-autoload (for resetting autoload file)
step 3 : remove the last entry from migration(to modify your database)`
The reason you are not able to migrate although you dont have existing tables in database is because your previous migrations still exist. Either clear the history or perform the above steps
To make a long story short, I have completely messed up my Laravel migrations on my local machine. They are 100% unusable.
I'm working with Laravel 5 for the first time, so I'm just messing with stuff and testing the waters, so to speak. Between manually tinkering with the database, rewriting my migrations, accidentally deleting a table or two (then the 'migrations' table itself [doh!]), I'm in this mixed-up state, and I just want to start all of the migration stuff over from scratch. However, I can't seem to figure out how to do that.
I'm currently stuck in a state where I can't do anything.
For example, if any remnants of old tables are still in the database when I perform php artisan migrate:refresh, I get a Base table or view already exists error message. However, if I delete all the tables, I get this error:
Next exception 'Illuminate\Database\QueryException' with message
'SQLSTATE[42S02]: Base table or view not found: 1146 Table
'bsd_status.projects' doesn't exist (SQL: select * from `projects`)' in
path/to/src/Illuminate/Database/Connection.php:620
I've run the following commands:
$ php artisan clear-compiled
$ php artisan cache:clear
$ php composer dump-autoload
$ php artisan migrate:install
I'm not even sure I'm doing this stuff in the right order. Anyway, other than completely reinstalling Laravel, how does one get all his/her migrations back to "out-of-the-box?" Any help would be greatly appreciated. Thanks!
What I liked to do is manually delete all the tables with what ever tool you use on your device. For me I just use phpmyadmin. After that I do.
php artisan migrate:install
php artisan migrate:refresh
Don't know if this is the official way, but it works every time.
If you don't want to use phpmyadmin you can just
login to mysql via command line
mysql -u root -p
DROP DATABASE laraveldb;
CREATE DATABASE laraveldb;
Although #Goddard's answer and #nozzleman's comment were both really helpful (I've used both suggestions several other times, so thank you), the solution had nothing to do with migrations. Well, … other than the fact that I screwed them up to begin with.
Anyway, nothing I did fixed the issue, so I put on my "think really f'ing hard" hat. After cursing for several minutes, I realized something. It appeared that — even when simply running artisan from the command line — any routes or providers I had set up were attempting to be "resolved" (or whatever the proper terminology is). Thus, I must have had a call somewhere attempting to get data from the missing table when the app started the bootstrap/init/start/run phase.
I decided to make sure I didn't have any weird things going on in my actual code, so I checked in my routes file (app/Http/routes.php) and all my Service Provider files (app/Providers/*) to see if I was trying to retrieve model data in any of them. Lo and behold, I opened app/Providers/AppServiceProvider.php and found this:
AppServiceProvider.php
public function boot()
{
$layout = 'default';
if ( Request::has('layout') ) {
$layout = Request::input('layout');
}
view()->share('sharedAppData', [
'layout' => $layout,
'projects' => App\Project::all() // <- WTF, mate?
]);
}
If you recall, the table being complained about in the error messages was named "projects." Thus, I was attempting to get all projects at boot, so no matter what I did (at the command line or otherwise), nothing was going to work because my Eloquent-extended model (App\Project) was looking for a table that simply didn't exist anymore.
The moral of the story: Laravel is super complex, I suck at it, and no matter how many times I try to follow the teachings of the great Jeffery Way, I'll forever be a Laran00b.
try this:
composer dump-autoload
composer clear-cache
I'm working on a CMS and I have a little problem with my migrations. I added a new migration file and I wanted to add that one. That didn't work so I ran this bit:
php artisan migrate:reset
After that I ran this bit:
php artisan migrate:install
php artisan migrate
And now I get this error:
{"error":{"type":"Illuminate\\Database\\QueryException","message":"SQLSTATE[42S02]: Base table or
view not found:1146 Table 'cms.pages' doesn't exist (SQL: select * from `pages`)"
The error kinda tells me that it can't find the database, because that's true.
I also have a command that runs the migrate and I run that one like this:
php artisan app:install
But that shows the same error...
Remove any lines requesting data from your model from these files to be sure artisan is not trying to load data from your non-existent table:
bootstrap/start.php
app/start/global.php
app/start/local.php
app/routes.php
Also be sure to un-register any service providers that utilize data from that table in their register or boot methods inside of app/config/app.php.
The issue is that these files not only get executed for browser (web) requests, but for all requests, including command-line artisan invocations (e.g. php artisan migrate). So if you try to use something before it is available in any of these files, you are going to have a Bad Time.
You can use this to dictate when your app is running from the console.
I believe this issue only occurs when you run a command
if( !App::runningInConsole() ){
//allow laravel-menu to run
}
This way you will prevent data load from your non-existent table