Skip to content

Instantly share code, notes, and snippets.

@joe-niland
Created April 18, 2017 06:38
Show Gist options
  • Save joe-niland/229cdab7c45059bbb92ac858175d2249 to your computer and use it in GitHub Desktop.
Save joe-niland/229cdab7c45059bbb92ac858175d2249 to your computer and use it in GitHub Desktop.
<?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