Skip to content

Commit

Permalink
permission pivot model
Browse files Browse the repository at this point in the history
  • Loading branch information
fzldn committed Sep 17, 2024
1 parent ad8bd38 commit 4f8dacb
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
38 changes: 36 additions & 2 deletions app/Models/PermissionRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,43 @@
namespace App\Models;

use App\Models\Traits\LogsModel;
use Illuminate\Database\Eloquent\Relations\MorphPivot;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\Pivot;

class PermissionRole extends MorphPivot
class PermissionRole extends Pivot
{
use LogsModel;

/**
* Get the table associated with the model.
*
* @return string
*/
public function getTable()
{
return config('permission.table_names.role_has_permissions', parent::getTable());
}

/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = true;

/**
* @return BelongsTo<Role>
*/
public function role(): BelongsTo
{
return $this->belongsTo(Role::class);
}

/**
* @return BelongsTo<Permission>
*/
public function permission(): BelongsTo
{
return $this->belongsTo(Permission::class);
}
}
35 changes: 35 additions & 0 deletions app/Models/RoleUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,44 @@
namespace App\Models;

use App\Models\Traits\LogsModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphPivot;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class RoleUser extends MorphPivot
{
use LogsModel;

/**
* Get the table associated with the model.
*
* @return string
*/
public function getTable()
{
return config('permission.table_names.model_has_roles', parent::getTable());
}

/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = true;

/**
* @return BelongsTo<Role>
*/
public function role(): BelongsTo
{
return $this->belongsTo(Role::class);
}

/**
* @return MorphTo<User>
*/
public function user(): MorphTo
{
return $this->morphTo('model');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public function up(): void
});

Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames, $pivotRole, $teams) {
$table->bigIncrements('id');
$table->unsignedBigInteger($pivotRole);

$table->string('model_type');
Expand All @@ -94,12 +95,13 @@ public function up(): void
$table->primary([$columnNames['team_foreign_key'], $pivotRole, $columnNames['model_morph_key'], 'model_type'],
'model_has_roles_role_model_type_primary');
} else {
$table->primary([$pivotRole, $columnNames['model_morph_key'], 'model_type'],
$table->unique([$pivotRole, $columnNames['model_morph_key'], 'model_type'],
'model_has_roles_role_model_type_primary');
}
});

Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames, $pivotRole, $pivotPermission) {
$table->bigIncrements('id');
$table->unsignedBigInteger($pivotPermission);
$table->unsignedBigInteger($pivotRole);

Expand All @@ -113,7 +115,7 @@ public function up(): void
->on($tableNames['roles'])
->onDelete('cascade');

$table->primary([$pivotPermission, $pivotRole], 'role_has_permissions_permission_id_role_id_primary');
$table->unique([$pivotPermission, $pivotRole], 'role_has_permissions_permission_id_role_id_primary');
});

app('cache')
Expand Down

0 comments on commit 4f8dacb

Please sign in to comment.