I’ve ran into a problem with some queries in Laravel due to the fact that some methods in query.php have a hard coded id as a column. Hence, if you use some other name than id in your database, these functions will fail miserably.
In my case I have an existing MSSQL database were the identity column of the user table is UserId not id. In my case the problem arises after a user has logged in – the script barfs with:
SELECT TOP 1 [userid] FROM [User] WHERE [id] = ?
Fluent seems to call one of those problematics functions, find in query.php, when trying to retrieve user data. Has anyone addressed this problem and if so how did you deal with?
UPDATE:
I do use Fluent as Auth driver and the error is triggered when doing Auth::check() or Auth::guest(). The stack trace looks like this:
#0 C:\laravel\database\connection.php(183): Laravel\Database\Connection->execute('SELECT TOP 1 [u...', Array)
#1 C:\laravel\database\query.php(709): Laravel\Database\Connection->query('SELECT TOP 1 [u...', Array)
#2 C:\laravel\database\query.php(653): Laravel\Database\Query->get(Array)
#3 C:\laravel\database\query.php(624): Laravel\Database\Query->first(Array)
#4 C:\laravel\auth\drivers\fluent.php(21): Laravel\Database\Query->find('162')
#5 C:\laravel\auth\drivers\driver.php(79): Laravel\Auth\Drivers\Fluent->retrieve('162')
#6 C:\laravel\auth\drivers\driver.php(65): Laravel\Auth\Drivers\Driver->user()
#7 C:\laravel\auth\drivers\driver.php(55): Laravel\Auth\Drivers\Driver->check()
There are 2 solutions to your problem:
Do not use Eloquent (I inferred from your question that that’s what you were using). Instead, use Fluent, e.g.
Though this approach is restrictive and, well, prevents you from using Eloquent.
Define your model like this:
where
$keyis the name of your primary key and$tableis the name of your table (useful if it isn’t the plural of the model name). This looks great I guess but unfortunately it may not be enough if you use relationships. Have a look at this workaround for more info.