In laravel, inside the config/auth.php
we have this by default
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
My question is what is the difference between these two drivers?
if I change the driver to database does that mean i can't use eloquent anymore?
There is a difference: eloquent uses an ORM abstraction over a table, while database uses direct SQL queries. If you intend to have a custom Auth class, the ORM abstraction is easier to extend. If you (just have tables and you don't intend to extend your Auth layer) or (you don't use Eloquent for anything else), then database is a good choice.
The technical reason for this difference is subtle, and perhaps surprising: a Laravel-based application need not use the Eloquent ORM. In that usage, the application might still want to use the Laravel-provided Authentication facade, so Laravel needs a different way of storing and retrieving the authentication data: hence the database driver.
From the Laravel documentation:
If your application is not using Eloquent, you may use the database authentication driver which uses the Laravel query builder.
Most Laravel-based applications use Eloquent, so the default is to use the Eloquent Auth class. If you change the driver to database, you can still use Eloquent in your app, though I can't imagine a use case for this combination.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With