How to Use Laravel Permission Package by Spatie with Multiple Database Connections?
I plan to use laravel permission package by Spatie with multiple database connections. Let's say I have two connections: master and default.
I want to use the users and roles tables from the "master" database connection and other tables such as permissions, model_has_roles, model_has_permissions and role_has_permissions from the default database connection.
I have created User and Role models like this:
User.php
<?php
namespace App\Models;
use App\Traits\HasUuid;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Models\Role;
use Laravel\Jetstream\HasProfilePhoto;
use Spatie\Permission\Traits\HasRoles;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasApiTokens;
use HasFactory;
use HasProfilePhoto;
use Notifiable;
use TwoFactorAuthenticatable;
use HasRoles;
use HasUuid;
protected $connection = 'master';
}
Role.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role as ModelsRole;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Role extends ModelsRole
{
use HasFactory;
protected $connection = 'master';
protected $fillable = [
'name',
'guard_name',
];
}
With the method above, I have successfully retrieved the users and roles data from the "master" database connection, but the permissions loaded are also from the "master" database connection.
I want the permissions data retrieved from the "default" database connection.
How can I load the permissions data from the "default" database connection?
Please or to participate in this conversation.