Laravel testing database migration fails due to Passport - php

I installed Laravel Passport and migrated the new tables just fine in my database. But when I try to run migrations for the testing database, ie. artisan migrate --database=testing I get:
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'oauth_auth_codes' already exists
The only table that exists in the testing database is the migrations table. It looks as if it's checking the regular database and seeing this Passport table instead of checking the testing database. Did I miss something simple here?

php artisan ... commands will use your .env variables for configuration, so yes, unless you specified which database connection to use, it will use your default .env's DB_ variables.
You can define something like a .env.testing, then re-run as php artisan migrate --env=testing to use that file instead of your default one.
.env:
DB_CONNECTION=mysql
DB_DATABASE=database
...
# Host, Port, Username, Password,
.env.testing:
DB_CONNECTION=mysql
DB_DATABASE=testing
...
# Host, Port, Username, Password,

Related

Laravel - User Access denied

I created my first App, using Laravel in the Backend. Everything was running on my localhost and I deployed everything to my Netcup Webhosting Server.
I run composer install successfully, generated the API key by using php artisan key:generate --ansi and set up my .env file. The File looks like:
APP_NAME="App Name"
APP_ENV=prod
APP_KEY=base64_key
APP_DEBUG=false
APP_URL=https://xx.xxxxx.de
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_name
DB_USERNAME="db_user"
DB_PASSWORD="db_user_password"
The I want to create all the database tables by using php artisan migrate. I tripple checked the values set in the .env and red a lot of solutions of other posts, but nothing worked. I keep getting the following error:
I already added quotes to DB_User and Pasword in .env file. I restarted the server several times and cleaned the cache.
It looks like some problems with auth on the Netcup Web hosting Server. Can you check list of user with access to db_name? Besides, by screenshoot I guessing that hosting add prefix ("k177581_") for db_name and db_user
Solved the issue:
As #N69S correctly suggested it was related to Netcup. For any other Netcup user the following solution.
Netcup devides its Webhosting and Database. Therefore you need to put you Database IP into your .env file. That can be found in the connection data.

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel9rentalcars.categories' doesn't exist

this what laravel give me when iam execute my laravel "code"
what can i do ?
enter image description here
Debug:
Your Table exist or not
If Table not exists then Run migration php artisan migrate
Check you have selected right DB
Share more information like screenshot and what you have tried.., etc
First, check DB_DATABASE in your .env file or database.php in the config folder for the right DB name. if it is ok, then check whether the categories table exists in the databases directory or not (even check the spelling). if it is not, you may create the table by running this command:
php artisan make:migration create_categories_table --create=categories
and then
php artisan migrate
check categories table , if exist then check actual query.

Laravel 6 config()->get('database.connections.mysql') not matching DB:connection()

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');

how do I fix sql error in laravel?

How can I fix this error? I searched for several forums, but I could not solve it! SQLSTATE [HY000] [1045] Access denied for user 'user' # 'localhost' (using SQL: select * from table where column = number)
A database consists of tables. You will populate the tables of the database using
php artisan migrate --seed
However, you need to create a database first, and use the database name for DB_DATABASE. Creating the database is independent of Laravel. See https://medium.com/#connorleech/build-an-online-forum-with-laravel-initial-setup-and-seeding-part-1-a53138d1fffc for some information on creating a database.
Make sure your .env file has the correct database parameters. e.g.:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=name_of_database
DB_USERNAME=username
DB_PASSWORD=password

How to fix "Database (database.sqlite) does not exist." in Laravel?

I'm studying laravel then I switched mysql from sqlite. I deleted the database.sqlite file, configure the database.php to mysql and .env file.
I'm having a error "Database (database.sqlite) does not exist". How to fix this error?
The sqlite driver doesn't create the database file if it doesn't exist.
The simple solution is to create an empty file just before migrating the database into it (filling the file with schemas and data)
Simply put, this command will fix it:
touch database/database.sqlite
You don't need anything in the .env file except the DB_CONNECTION key/value pair. When you are using SQLite, remove all of the following keys:
DB_HOST
DB_PORT
DB_DATABASE
DB_USERNAME
DB_PASSWORD
If the above solution can not handle the current issue. click here
Issue with SQLite and Laravel on Heroku

Categories