Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions src/Entrust/Traits/EntrustPermissionTrait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php namespace Zizaco\Entrust\Traits;
<?php

namespace Zizaco\Entrust\Traits;

use Illuminate\Support\Facades\Config;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
* This file is part of Entrust,
Expand All @@ -7,38 +12,32 @@
* @license MIT
* @package Zizaco\Entrust
*/

use Illuminate\Support\Facades\Config;

trait EntrustPermissionTrait
{
/**
* Many-to-Many relations with role model.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function roles()
public function roles(): BelongsToMany
{
return $this->belongsToMany(Config::get('entrust.role'), Config::get('entrust.permission_role_table'), Config::get('entrust.permission_foreign_key'), Config::get('entrust.role_foreign_key'));
return $this->belongsToMany(
Config::get('entrust.role'),
Config::get('entrust.permission_role_table'),
Config::get('entrust.permission_foreign_key'),
Config::get('entrust.role_foreign_key')
);
}

/**
* Boot the permission model
* Attach event listener to remove the many-to-many records when trying to delete
* Boot the permission model.
* Attach event listener to remove the many-to-many records when trying to delete.
* Will NOT delete any records if the permission model uses soft deletes.
*
* @return void|bool
*/
public static function boot()
public static function bootEntrustPermissionTrait(): void
{
parent::boot();

static::deleting(function($permission) {
if (!method_exists(Config::get('entrust.permission'), 'bootSoftDeletes')) {
$permission->roles()->sync([]);
static::deleting(function ($permission) {
if (! method_exists(Config::get('entrust.permission'), 'bootSoftDeletes')) {
$permission->roles()->detach();
}

return true;
});
}
}