Hi,
I'm using UUID as primary key for all of my project tables, how can i disable autoincrement and allow roles and permissions primary key ID to be fillable? i have managed to do it on other tables but i'm stacked with these two.
with regards
A couple references which may help you:
model_morph_key in your config/permission.php file to match your custom field namethis issue has been solved by someelse in stackoverflow platform
The link to the solution is Link to solution
Thanks for posting the link.
Hi, i was try implementing UUID on this package at Laravel 5.7 and worked.
My migration:
public function up()
{
$tableNames = config('permission.table_names');
$columnNames = config('permission.column_names');
if (!Schema::hasTable($tableNames['permissions'])) {
Schema::create($tableNames['permissions'], function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name');
$table->string('guard_name');
$table->timestamps();
});
}
if (!Schema::hasTable($tableNames['roles'])) {
Schema::create($tableNames['roles'], function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name');
$table->string('guard_name');
$table->timestamps();
});
}
if (!Schema::hasTable($tableNames['model_has_permissions'])) {
Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) {
$table->uuid('permission_id');
$table->string('model_type');
$table->uuid($columnNames['model_morph_key']);
$table->index([$columnNames['model_morph_key'], 'model_type']);
$table->foreign('permission_id')
->references('id')
->on($tableNames['permissions'])
->onDelete('cascade');
$table->primary(['permission_id', $columnNames['model_morph_key'], 'model_type'],
'model_has_permissions_permission_model_type_primary');
});
}
if (!Schema::hasTable($tableNames['model_has_roles'])) {
Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) {
$table->uuid('role_id');
$table->string('model_type');
$table->uuid($columnNames['model_morph_key']);
$table->index([$columnNames['model_morph_key'], 'model_type', ]);
$table->foreign('role_id')
->references('id')
->on($tableNames['roles'])
->onDelete('cascade');
$table->primary(['role_id', $columnNames['model_morph_key'], 'model_type'],
'model_has_roles_role_model_type_primary');
});
}
if (!Schema::hasTable($tableNames['role_has_permissions'])) {
Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
$table->uuid('permission_id');
$table->uuid('role_id');
$table->foreign('permission_id')
->references('id')
->on($tableNames['permissions'])
->onDelete('cascade');
$table->foreign('role_id')
->references('id')
->on($tableNames['roles'])
->onDelete('cascade');
$table->primary(['permission_id', 'role_id']);
app('cache')
->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null)
->forget(config('permission.cache.key'));
});
}
}
My AppServiceProvider.php :
...
use Illuminate\Support\ServiceProvider;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Ramsey\Uuid\Uuid;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
/* Begin : UUID Adjustment */
Permission::retrieved(function (Permission $permission) {
$permission->incrementing = false;
});
Permission::creating(function (Permission $permission) {
$permission->incrementing = false;
$permission->id = Uuid::uuid4()->toString();
});
Role::retrieved(function (Role $role) {
$role->incrementing = false;
});
Role::creating(function (Role $role) {
$role->incrementing = false;
$role->id = Uuid::uuid4()->toString();
});
/* End : UUID Adjustment */
...
}
...
}
Most helpful comment
Hi, i was try implementing UUID on this package at Laravel 5.7 and worked.
My migration:
My AppServiceProvider.php :