I'm working on a laravel app that used to work, but I don't know why after a few modifications on different setups, I cannot make it work as expected locally.
Symptoms
Queries from models accessing incorrect table names
My DB contains tables prefixed by sub_ but queries made from controllers are accessing non-prefixed tables.
For instance User::where(User::EMAIL_ADDRESS, "=", mb_convert_case($validated[User::EMAIL_ADDRESS], MB_CASE_LOWER))->first(); gives an error (table users does not exist).
Migrations accessing prefixed table names
Migrations are applied on the right table names
Model->getTable() returns names without prefix
When executing this code with php artisan tinker, the returned table name does not contain the prefix, even though it is correctly retrieved from the config:
php artisan tinker
> dd(['config_prefix' => config('database.connections.sqlite.prefix'), 'db_prefix' => \Illuminate\Support\Facades\DB::connection()->getTablePrefix(), 'env_prefix' => env('DB_PREFIX'), 'model_
table' => (new App\Season)->getTable()]);
array:5 [ // vendor\psy\psysh\src\ExecutionLoopClosure.php(52) : eval()'d code:1
"config_prefix" => "sub_"
"db_prefix" => "sub_"
"env_prefix" => "sub_"
"model_table" => "seasons"
]
For information, none of my models contain a property that would override the table name (no $table or $prefix property).
Query dump: from seems correct, but querying does not work
php artisan tinker
Psy Shell v0.12.8 (PHP 8.2.8 — cli) by Justin Hileman
> dd([
. 'config_prefix' => config('database.connections.mysql.prefix'),
. 'db_prefix' => \Illuminate\Support\Facades\DB::connection()->getTablePrefix(),
. 'env_prefix' => env('DB_PREFIX'),
. 'model_table' => (new App\Season)->getTable(),
. 'query' => (new App\Season)->query()
. ]);
array:5 [ // vendor\psy\psysh\src\ExecutionLoopClosure.php(52) : eval()'d code:1
"config_prefix" => "sub_"
"db_prefix" => "sub_"
"env_prefix" => "sub_"
"model_table" => "sub_seasons"
"query" => Illuminate\Database\Eloquent\Builder^ {#6141
#query: Illuminate\Database\Query\Builder^ {#5340
+connection: Illuminate\Database\SQLiteConnection^ {#5411 …24}
+grammar: Illuminate\Database\Query\Grammars\SQLiteGrammar^ {#5412 …4}
+processor: Illuminate\Database\Query\Processors\SQLiteProcessor^ {#5413}
+from: "sub_seasons"
// [...]
}
#model: App\Season^ {#5381
#connection: null
#table: null
#primaryKey: "id"
// [...]
}
// [...]
}
]
Query from tinker works
When running App\Season::first() on tinker, the query is valid and it returns something.
The same code within a controller, throws an error about the table name being incorrect.
Failed Attempts
- Cache clearing: php artisan config:clear, cache:clear, etc.
- Override getTable(): Returns correct value but still ignored
- AppServiceProvider boot(): Various connection-level fixes
Environment:
- Laravel 9
- on Windows 11
- php 8.2.8
Dependencies:
"require": {
"php": "^8.1.0",
"bjeavons/zxcvbn-php": "^1.1",
"doctrine/dbal": "^2.10",
"fruitcake/laravel-cors": "^3.0",
"goldspecdigital/laravel-eloquent-uuid": "^9.0",
"guzzlehttp/guzzle": "^7.0.1",
"laravel/framework": "^9.0",
"laravel/tinker": "^2.0",
"phpoffice/phpspreadsheet": "1.27",
"sentry/sentry-laravel": "3.1.3",
"tymon/jwt-auth": "^2.0"
},
Config
# .env file
DB_PREFIX=sub_
DB_CONNECTION=sqlite
# config/database.php
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => env('DB_PREFIX', ''),
// ...
],
// ...
],
Does anyone has a clue on what's going on with my setup?
Thanks