-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I use UUID #948
Labels
Comments
drbyte
changed the title
How can to override the Role and Permissions models
How can I use UUID
Nov 20, 2018
A couple references which may help you:
|
this issue has been solved by someelse in stackoverflow platform |
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 */
...
}
...
} |
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
The text was updated successfully, but these errors were encountered: