| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- <?php
- /**
- * Created by PhpStorm.
- * User: sdf_sky
- * Date: 16/6/20
- * Time: 下午6:14
- */
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- class Role extends Model
- {
- protected $table = 'roles';
- protected $fillable = ['id', 'name','description','sort','slug'];
- public function permissions()
- {
- return $this->belongsToMany('App\Models\Permission');
- }
- /**
- * Determine if the role has the given permission.
- *
- * @param mixed $permission
- * @return boolean
- */
- public function inRole($permission)
- {
- if (is_string($permission)) {
- return $this->permissions->contains('name', $permission);
- }
- return !! $permission->intersect($this->permissions)->count();
- }
- /**
- * @param $permission
- * @return bool
- */
- public function attachPermission($permission)
- {
- return (!$this->permissions()->get()->contains($permission)) ? $this->permissions()->attach($permission) : true;
- }
- /**
- * @param $permission
- * @return mixed
- */
- public function detachPermission($permission)
- {
- return $this->permissions()->detach($permission);
- }
- /**
- * Detach all permissions.
- *
- * @return int
- */
- public function detachAllPermissions()
- {
- return $this->permissions()->detach();
- }
- }
|