Skip to content
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

Closed
antweny opened this issue Nov 20, 2018 · 4 comments · Fixed by #2089
Closed

How can I use UUID #948

antweny opened this issue Nov 20, 2018 · 4 comments · Fixed by #2089
Labels

Comments

@antweny
Copy link

antweny commented Nov 20, 2018

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

@drbyte drbyte added the support label Nov 20, 2018
@drbyte drbyte changed the title How can to override the Role and Permissions models How can I use UUID Nov 20, 2018
@drbyte
Copy link
Collaborator

drbyte commented Nov 20, 2018

A couple references which may help you:

  • You will need to alter the migration
  • Set the model_morph_key in your config/permission.php file to match your custom field name
  • While there have been code changes in the package since this discussion, you may find it helpful to your needs: Using UUid instead of Native ID's #793

@antweny
Copy link
Author

antweny commented Nov 21, 2018

this issue has been solved by someelse in stackoverflow platform
The link to the solution is Link to solution

@drbyte
Copy link
Collaborator

drbyte commented Nov 22, 2018

Thanks for posting the link.

@drbyte drbyte closed this as completed Nov 22, 2018
@warmansuganda
Copy link

warmansuganda commented Feb 19, 2019

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 */

        ...

    }
    
    ...

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants