Repository URL to install this package:
|
Version:
1.0.0 ▾
|
<?php namespace Modules\Users\Entities;
use Illuminate\Database\Eloquent\Model;
/**
* Modules\Users\Entities\Permission
*
* @property integer $id
* @property integer $inherit_id
* @property string $name
* @property string $slug
* @property string $description
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property-read \Illuminate\Database\Eloquent\Collection|\Modules\Users\Entities\Role[] $roles
* @property-read \Illuminate\Database\Eloquent\Collection|\Modules\Users\Entities\User[] $users
*/
class Permission extends Model
{
/**
* The attributes that are fillable via mass assignment.
*
* @var array
*/
protected $fillable = ['name', 'slug', 'description', 'inherit_id'];
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'permissions';
/**
* Permissions can belong to many roles.
*
* @return Model
*/
public function roles()
{
$model = config('acl.role', 'Modules\Users\Entities\Role');
return $this->belongsToMany($model)->withTimestamps();
}
/**
* Permissions can belong to many users.
*
* @return Model
*/
public function users()
{
return $this->belongsToMany(config('auth.model'))->withTimestamps();
}
/**
* @param $value
* @return array
*/
public function getSlugAttribute($value)
{
return json_decode($value, true);
}
/**
* @param $value
*/
public function setSlugAttribute($value)
{
// if nothing being set, clear slug
if (empty($value)) {
$this->attributes['slug'] = '[]';
return;
}
$value = is_array($value) ? $value : [$value => true];
// if attribute is being updated.
if (isset($this->original['slug'])) {
$value = $value + json_decode($this->original['slug'], true);
// sort by key
ksort($value);
}
// remove null values.
$value = array_filter($value, 'is_bool');
// store as json.
$this->attributes['slug'] = json_encode($value);
}
}