Created
April 18, 2017 06:38
-
-
Save joe-niland/229cdab7c45059bbb92ac858175d2249 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
trait Securable extends BaseModel | |
{ | |
public function usersWithAccess() { | |
return $this->morphedByMany('App\User', 'access'); | |
} | |
public function rolesWithAccess() { | |
return $this->morphedByMany('App\Role', 'access'); | |
} | |
} | |
class Document extends BaseModel implements HasMedia { | |
use Securable, HasMediaTrait, Eloquence; | |
// ... | |
} | |
class User extends Model | |
{ | |
/** | |
* Get all the documents accessible to this User. | |
*/ | |
public function availableDocuments() | |
{ | |
return $this->morphToMany('App\Document', 'access'); | |
} | |
public function editableDocuments() { | |
return $this->availableDocuments()->where('access_name', 'edit'); | |
} | |
public function viewableDocuments() { | |
return $this->availableDocuments()->where('access_name', 'view'); | |
} | |
// ... | |
} | |
class Role extends Model | |
{ | |
/** | |
* Get all the documents accessible to this Role. | |
*/ | |
public function availableDocuments() | |
{ | |
return $this->morphToMany('App\Document', 'access'); | |
} | |
public function editableDocuments() { | |
return $this->availableDocuments()->where('access_name', 'edit'); | |
} | |
public function viewableDocuments() { | |
return $this->availableDocuments()->where('access_name', 'view'); | |
} | |
// ... | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment