Custom guard

Custom guard

Starting with Voyager 1.2 you can define a (custom) guard which is used throughout Voyager. To do so, just bind the name of your auth-guard to VoyagerGuard. First, make sure you have defined a guard as per the Laravel documentation. After that open your AuthServiceProvider and add the following to the register method:
1
$this->app->singleton('VoyagerGuard', function () {
2
return 'your-custom-guard-name';
3
});
Copied!
Now this guard is used instead of the default guard.

Example - using a different model and table for Admins

First you have to create a new table. Let's call it admins:
1
<?php
2
Schema::create('admins', function (Blueprint $table) {
3
$table->bigIncrements('id');
4
$table->bigInteger('role_id')->unsigned()->nullable();
5
$table->string('name');
6
$table->string('email')->unique();
7
$table->string('avatar')->nullable()->default('users/default.png');
8
$table->string('password')->nullable();
9
$table->string('remember_token')->nullable();
10
$table->text('settings')->nullable()->default(null);
11
$table->timestamps();
12
$table->foreign('role_id')->references('id')->on('roles');
13
});
Copied!
and a model which extends Voyagers user-model:
1
<?php
2
3
namespace App;
4
5
class Admin extends \TCG\Voyager\Models\User
6
{
7
8
}
Copied!
Next, create a guard named admin in your config/auth.php:
1
'guards' => [
2
'admin' => [
3
'driver' => 'session',
4
'provider' => 'admins',
5
],
6
7
// ...
8
],
Copied!
And a user provider called admins:
1
'providers' => [
2
'admins' => [
3
'driver' => 'eloquent',
4
'model' => App\Admin::class,
5
],
6
7
// ...
8
],
Copied!
Next you have to tell Voyager to use your new guard. Open you AppServiceProvider.php and add the following to the register method:
1
public function register()
2
{
3
$this->app->singleton('VoyagerGuard', function () {
4
return 'admin';
5
});
6
}
Copied!
Please note that the user-bread is still responsible to edit users - not admins. Create a BREAD for the admins table if you want to change Admins.
Last modified 1mo ago