Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between Eloquent and database authentication drivers in laravel

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?

like image 653
Achraf Khouadja Avatar asked Oct 26 '25 08:10

Achraf Khouadja


1 Answers

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.

like image 60
bishop Avatar answered Oct 27 '25 21:10

bishop