From a fresh Statamic project
If you installed Statamic using the statamic new
command, or created a project based on the statamic/statamic
repo, it will be configured to store users in files.
Statamic comes with an Eloquent driver to make the transition as seamless as possible.
- Ensure you have a database configured.
- In your user model, cast the preferences column to json.
class User extends Authenticatable { protected function casts(): array // [tl! focus] { // [tl! focus] return [ // [tl! focus] 'preferences' => 'json', // [tl! ++] [tl! focus] ]; // [tl! focus] }; // [tl! focus] // ... }
- If you plan to import existing file based users, you'll need to use UUIDs for the primary key. You can do this by adding a trait to your user model:
class User extends Authenticatable { use \Illuminate\Database\Eloquent\Concerns\HasUuids; // [tl! ++] [tl! focus] // ... }
- In
config/statamic/users.php
, use the Eloquent repository.'repository' => 'file', // [tl! --] 'repository' => 'eloquent', // [tl! ++]
- In
config/auth.php
, use the Eloquent provider:'providers' => [ 'users' => [ 'driver' => 'statamic', // [tl! --] 'driver' => 'eloquent', // [tl! ++] 'model' => App\Models\User::class, // [tl! ++] ] ]
- Generate a migration for the role and user group pivot tables.
php please auth:migration
- If you're planning to import existing file based users, edit the migration to change the
id
&user_id
columns to theuuid
type.Schema::table('users', function (Blueprint $table) { $table->uuid('id')->change(); // [tl! ++] [tl! **] $table->boolean('super')->default(false); $table->string('avatar')->nullable(); $table->json('preferences')->nullable(); $table->timestamp('last_login')->nullable(); $table->string('password')->nullable()->change(); }); Schema::create('role_user', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id'); // [tl! --] [tl! **] $table->uuid('user_id'); // [tl! ++] [tl! **] $table->string('role_id'); }); Schema::create('group_user', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id'); // [tl! --] [tl! **] $table->uuid('user_id'); // [tl! ++] [tl! **] $table->string('group_id'); });
- If you've customized your
user
blueprint, edit the migration so it includes those fields as columns. You can also create a new migration file by runningphp artisan make:migration
. You'll have to manually edit the migration file to reflect your changes. Read up on Laravel database migrations here.$table->string('some_field');
- If you're planning to import existing file based users, edit the migration to change the
- Run the migrations:
php artisan migrate
- If you have existing file based users, import them:
php please eloquent:import-users
- If you are using the Statamic forgot password form, add the following method to your User model
public function sendPasswordResetNotification($token) { $this->notify(new \Statamic\Notifications\PasswordReset($token)); }
In an existing Laravel app
If you've installed Statamic into an existing Laravel app, it will already be configured to use the Eloquent driver.
You will need to run migrations to prepare your database for Statamic's user, password reset, and permission setup.
-
Configure the two separate password reset drivers. Unlike a regular Laravel installation, Statamic has a second table to track password activations which are the same as resets, but last a little longer before they expire. This is optional.
In
config/auth.php
add the following inside thepasswords
array:'activations' => [ 'provider' => 'users', 'table' => 'password_activation_tokens', 'expire' => 4320, 'throttle' => 60, ],
In
config/statamic/users.php
change thepasswords
array to:'passwords' => [ 'resets' => 'users', 'activations' => 'activations', ],
-
Create and run the migrations.
This will add some columns to the
users
table (likesuper
, andlast_login
), create therole_user
andgroup_user
pivot tables, and create thepassword_activations
table.php please auth:migration php artisan migrate
-
Optional: If you are using the Statamic forgot password form, add the following method to your User model
public function sendPasswordResetNotification($token) { $this->notify(new \Statamic\Notifications\PasswordReset($token)); }
This assumes you are happy to use our opinionated setup. If you need something more custom you can create your own user driver.