├── README.md ├── composer.json └── src ├── Classes └── Actions.php ├── Middleware ├── Entrusted.php ├── EntrustedAny.php ├── Permitted.php └── PermittedAny.php ├── Models ├── Actions │ ├── Bookmark.php │ ├── Comment.php │ ├── Favourite.php │ ├── Follow.php │ ├── Like.php │ ├── Review.php │ ├── Subscribe.php │ └── Vote.php ├── Permissions │ ├── Permissible.php │ ├── Permission.php │ └── PermissionTranslation.php ├── Roles │ ├── Role.php │ ├── RoleTranslation.php │ └── Roleable.php ├── UserAgent.php └── UserPasswordHistory.php ├── Traits ├── Actions │ ├── Bookmark │ │ ├── isBookmaker.php │ │ └── isBookmarkable.php │ ├── Comment │ │ ├── isCommentable.php │ │ └── isCommenter.php │ ├── Favourite │ │ ├── isFavorer.php │ │ └── isFavourable.php │ ├── Follow │ │ ├── isFollowable.php │ │ └── isFollower.php │ ├── Like │ │ ├── isLikeable.php │ │ └── isLiker.php │ ├── Review │ │ ├── isReviewable.php │ │ └── isReviewer.php │ ├── Subscribe │ │ ├── isSubscribable.php │ │ └── isSubscriber.php │ └── Vote │ │ ├── isVotable.php │ │ └── isVoter.php ├── HasDevices.php ├── HasPermissions.php ├── HasRoles.php └── Password │ ├── HasHashedPassword.php │ └── HasPasswordHistory.php ├── UsersServiceProvider.php ├── database └── migrations │ ├── 2021_02_01_000024_create_user_agents_table.php │ ├── 2021_02_01_000025_create_user_password_history_table.php │ ├── 2021_02_01_000026_create_permissions_table.php │ ├── 2021_02_01_000027_create_permission_translations_table.php │ ├── 2021_02_01_000028_create_permissibles_table.php │ ├── 2021_02_01_000029_create_roles_table.php │ ├── 2021_02_01_000030_create_role_translations_table.php │ ├── 2021_02_01_000031_create_roleables_table.php │ ├── 2021_02_01_000077_create_likes_table.php │ ├── 2021_02_01_000078_create_follows_table.php │ ├── 2021_02_01_000079_create_favourites_table.php │ ├── 2021_02_01_000080_create_bookmarks_table.php │ ├── 2021_02_01_000081_create_votes_table.php │ ├── 2021_02_01_000082_create_reviews_table.php │ ├── 2021_02_01_000083_create_subscribes_table.php │ └── 2021_02_01_000084_create_comments_table.php └── helpers.php /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |

4 | PHP Version : >= 7.2 5 | Laravel Version : >= 6.0 6 | License 7 |
8 | Source 9 | Packagist Version 10 | Packagist Downloads 11 |

12 | 13 |
14 | 15 |
LARAVEL USERS
16 | 17 |

18 | Roles & Permissions Devices 19 | Password Hashing Password History 20 |

21 | 22 |

23 | Likes Followers Subscriptions 24 | Reviews Rates Favourites Bookmarks 25 |

26 | 27 | ## Documentation 28 | 29 | You can find the detailed documentation here in [Laravel Users Documentation](https://pharaonic.io/packages/laravel/users). 30 | 31 | ## Contributing 32 | 33 | Thank you for considering contributing to this package! Be one of Pharaonic team. 34 | 35 | ## License 36 | 37 | This package is an open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT). 38 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pharaonic/laravel-users", 3 | "description": "Laravel Users (Roles & Permissions, Devices, Password: Auto Hashing & History, Actions: Likes, Rates, Subscribes, Votes, Follows, Favorites, Bookmarks).", 4 | "keywords": [ 5 | "laravel roles", 6 | "laravel permissions", 7 | "laravel user devices", 8 | "laravel user passwords", 9 | "laravel user passwords hashed", 10 | "laravel user passwords history", 11 | "laravel likes", 12 | "laravel rates", 13 | "laravel subscribes", 14 | "laravel subscribers", 15 | "laravel votes", 16 | "laravel follows", 17 | "laravel followers", 18 | "laravel favourties", 19 | "laravel bookmarks", 20 | "laravel", 21 | "php" 22 | ], 23 | "license": "MIT", 24 | "authors": [ 25 | { 26 | "name": "Moamen Eltouny (Raggi)", 27 | "email": "support@raggitech.com" 28 | } 29 | ], 30 | "require": { 31 | "php": ">=7.2", 32 | "laravel/framework": ">=6.0", 33 | "pharaonic/laravel-agents-detector": "^1.0.3", 34 | "pharaonic/laravel-translatable": "^2.0.0" 35 | }, 36 | "config": { 37 | "sort-packages": true 38 | }, 39 | "extra": { 40 | "laravel": { 41 | "providers": [ 42 | "Pharaonic\\Laravel\\Users\\UsersServiceProvider" 43 | ] 44 | } 45 | }, 46 | "autoload": { 47 | "psr-4": { 48 | "Pharaonic\\Laravel\\Users\\": "src" 49 | }, 50 | "files": ["src/helpers.php"] 51 | }, 52 | "minimum-stability": "dev", 53 | "prefer-stable": true 54 | } -------------------------------------------------------------------------------- /src/Classes/Actions.php: -------------------------------------------------------------------------------- 1 | check() ? false : auth()->user()->{$action}(self::prepareParamsArray($list)); 12 | } 13 | 14 | /** 15 | * Prepare Roles & Permissions 16 | * 17 | * @param array $params 18 | * @return array 19 | */ 20 | private static function prepareParamsArray($params) 21 | { 22 | if (is_array($params[0])) $params = $params[0]; 23 | if (is_string($params)) $params = explode(',', $params); 24 | 25 | return $params; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Middleware/Entrusted.php: -------------------------------------------------------------------------------- 1 | user(); 21 | 22 | if (!$user || empty($roles) || !$user->entrusted($roles)) return abort(404); 23 | 24 | return $next($request); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Middleware/EntrustedAny.php: -------------------------------------------------------------------------------- 1 | user(); 21 | 22 | if (!$user || empty($roles) || !$user->entrustedAny($roles)) return abort(404); 23 | 24 | return $next($request); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Middleware/Permitted.php: -------------------------------------------------------------------------------- 1 | user(); 21 | if (!$user || empty($permissions) || !$user->permitted($permissions)) return abort(404); 22 | 23 | return $next($request); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Middleware/PermittedAny.php: -------------------------------------------------------------------------------- 1 | user(); 21 | if (!$user || empty($permissions) || !$user->permittedAny($permissions)) return abort(404); 22 | 23 | return $next($request); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Models/Actions/Bookmark.php: -------------------------------------------------------------------------------- 1 | 'array']; 26 | 27 | /** 28 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 29 | */ 30 | public function bookmarkable() 31 | { 32 | return $this->morphTo(); 33 | } 34 | 35 | /** 36 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 37 | */ 38 | public function bookmarker() 39 | { 40 | return $this->morphTo(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Models/Actions/Comment.php: -------------------------------------------------------------------------------- 1 | morphTo(); 31 | } 32 | 33 | /** 34 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 35 | */ 36 | public function commenter() 37 | { 38 | return $this->morphTo(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Models/Actions/Favourite.php: -------------------------------------------------------------------------------- 1 | morphTo(); 25 | } 26 | 27 | /** 28 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 29 | */ 30 | public function favorer() 31 | { 32 | return $this->morphTo(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Models/Actions/Follow.php: -------------------------------------------------------------------------------- 1 | morphTo(); 25 | } 26 | 27 | /** 28 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 29 | */ 30 | public function follower() 31 | { 32 | return $this->morphTo(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Models/Actions/Like.php: -------------------------------------------------------------------------------- 1 | morphTo(); 25 | } 26 | 27 | /** 28 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 29 | */ 30 | public function liker() 31 | { 32 | return $this->morphTo(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Models/Actions/Review.php: -------------------------------------------------------------------------------- 1 | 'float', 28 | 'published' => 'boolean' 29 | ]; 30 | 31 | /** 32 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 33 | */ 34 | public function reviewable() 35 | { 36 | return $this->morphTo(); 37 | } 38 | 39 | /** 40 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 41 | */ 42 | public function reviewer() 43 | { 44 | return $this->morphTo(); 45 | } 46 | 47 | /** 48 | * Publish This Rate 49 | * 50 | * @return boolean 51 | */ 52 | public function publish() 53 | { 54 | return $this->update(['published' => true]); 55 | } 56 | 57 | /** 58 | * Unpublish This Rate 59 | * 60 | * @return boolean 61 | */ 62 | public function unpublish() 63 | { 64 | return $this->update(['published' => false]); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/Models/Actions/Subscribe.php: -------------------------------------------------------------------------------- 1 | morphTo(); 25 | } 26 | 27 | /** 28 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 29 | */ 30 | public function subscriber() 31 | { 32 | return $this->morphTo(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Models/Actions/Vote.php: -------------------------------------------------------------------------------- 1 | 'boolean']; 26 | 27 | /** 28 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 29 | */ 30 | public function votable() 31 | { 32 | return $this->morphTo(); 33 | } 34 | 35 | /** 36 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 37 | */ 38 | public function voter() 39 | { 40 | return $this->morphTo(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Models/Permissions/Permissible.php: -------------------------------------------------------------------------------- 1 | 13 | */ 14 | class Permissible extends Model 15 | { 16 | /** 17 | * The attributes that are mass assignable. 18 | * 19 | * @var array 20 | */ 21 | protected $fillable = ['permission_id']; 22 | 23 | /** 24 | * Indicates if the model should be timestamped. 25 | * 26 | * @var bool 27 | */ 28 | public $timestamps = false; 29 | 30 | /** 31 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo 32 | */ 33 | public function permission() 34 | { 35 | return $this->belongsTo(Permission::class); 36 | } 37 | 38 | /** 39 | * Get the parent model 40 | */ 41 | public function permissible() 42 | { 43 | return $this->morphTo(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Models/Permissions/Permission.php: -------------------------------------------------------------------------------- 1 | 15 | */ 16 | class Permission extends Model 17 | { 18 | use Translatable; 19 | 20 | /** 21 | * The attributes that are mass assignable. 22 | * 23 | * @var array 24 | */ 25 | protected $fillable = ['code']; 26 | 27 | /** 28 | * Translatable attributes names. 29 | * 30 | * @var array 31 | */ 32 | protected $translatableAttributes = ['title']; 33 | 34 | /** 35 | * The "booted" method of the model. 36 | * 37 | * @return void 38 | */ 39 | protected static function booted() 40 | { 41 | static::deleting(function ($permission) { 42 | $permission->permissibles()->delete(); 43 | }); 44 | } 45 | 46 | /** 47 | * Getting permissions by code 48 | * 49 | * @param string $code 50 | * @return static|null 51 | */ 52 | public static function findByCode(string ...$code) 53 | { 54 | $result = static::with('translations')->whereIn('code', $code); 55 | return count($code) > 1 ? $result->get() : $result->limit(1)->first(); 56 | } 57 | 58 | /** 59 | * Create a new permissions 60 | * 61 | * @param string $code 62 | * @param array|string $title 63 | * @param string|null $locale 64 | * @return Permission 65 | */ 66 | public static function set(string $code, $title, string $locale = null) 67 | { 68 | $permission = new self; 69 | $data = ['code' => $code]; 70 | 71 | $localKey = $permission->translationsKey ?? 'locale'; 72 | 73 | if (is_array($title)) 74 | $data[$localKey] = $title; 75 | else 76 | $data[$localKey][$locale ?? app()->getLocale()]['title'] = $title; 77 | 78 | $permission->fill($data)->save(); 79 | 80 | return $permission; 81 | } 82 | 83 | /** 84 | * Get all attached Permissibles to the permission. 85 | * 86 | * @return \Illuminate\Database\Eloquent\Relations\hasMany 87 | */ 88 | public function permissibles() 89 | { 90 | return $this->hasMany(Permissible::class); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/Models/Permissions/PermissionTranslation.php: -------------------------------------------------------------------------------- 1 | 16 | */ 17 | class Role extends Model 18 | { 19 | use Translatable; 20 | use HasPermissions; 21 | 22 | /** 23 | * The attributes that are mass assignable. 24 | * 25 | * @var array 26 | */ 27 | protected $fillable = ['code']; 28 | 29 | /** 30 | * Translatable attributes names. 31 | * 32 | * @var array 33 | */ 34 | protected $translatableAttributes = ['title']; 35 | 36 | /** 37 | * The "booted" method of the model. 38 | * 39 | * @return void 40 | */ 41 | protected static function booted() 42 | { 43 | static::deleting(function ($role) { 44 | $role->roleables()->delete(); 45 | }); 46 | } 47 | 48 | /** 49 | * Getting roles by code 50 | * 51 | * @param string $code 52 | * @return static|null 53 | */ 54 | public static function findByCode(string ...$code) 55 | { 56 | $result = static::with('translations')->whereIn('code', $code); 57 | return count($code) > 1 ? $result->get() : $result->limit(1)->first(); 58 | } 59 | 60 | /** 61 | * Create a new role 62 | * 63 | * @param string $code 64 | * @param array|string $title 65 | * @param array $permissions 66 | * @param string|null $locale 67 | * @return Role 68 | */ 69 | public static function set(string $code, $title, array $permissions = [], string $locale = null) 70 | { 71 | $role = new self; 72 | $data = ['code' => $code]; 73 | 74 | $localKey = $role->translationsKey ?? 'locale'; 75 | 76 | if (is_array($title)) 77 | $data[$localKey] = $title; 78 | else 79 | $data[$localKey][$locale ?? app()->getLocale()]['title'] = $title; 80 | 81 | $role->fill($data)->save(); 82 | 83 | if (!empty($permissions)) 84 | $role->syncPermissions($permissions); 85 | 86 | return $role; 87 | } 88 | 89 | /** 90 | * Get all attached roleables to the role. 91 | * 92 | * @return \Illuminate\Database\Eloquent\Relations\hasMany 93 | */ 94 | public function roleables() 95 | { 96 | return $this->hasMany(Roleable::class); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/Models/Roles/RoleTranslation.php: -------------------------------------------------------------------------------- 1 | 13 | */ 14 | class Roleable extends Model 15 | { 16 | /** 17 | * The attributes that are mass assignable. 18 | * 19 | * @var array 20 | */ 21 | protected $fillable = ['role_id']; 22 | 23 | /** 24 | * Indicates if the model should be timestamped. 25 | * 26 | * @var bool 27 | */ 28 | public $timestamps = false; 29 | 30 | /** 31 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo 32 | */ 33 | public function role() 34 | { 35 | return $this->belongsTo(Role::class); 36 | } 37 | 38 | /** 39 | * Get the parent model 40 | */ 41 | public function roleable() 42 | { 43 | return $this->morphTo(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Models/UserAgent.php: -------------------------------------------------------------------------------- 1 | 22 | */ 23 | class UserAgent extends Model 24 | { 25 | /** 26 | * The attributes that are mass assignable. 27 | * 28 | * @var array 29 | */ 30 | protected $fillable = ['agent_id', 'signature', 'fcm_token', 'ip', 'user_id', 'user_type', 'last_action_at']; 31 | 32 | /** 33 | * {@inheritDoc} 34 | */ 35 | protected $casts = ['last_action_at' => 'datetime']; 36 | 37 | /** 38 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo 39 | */ 40 | public function agent() 41 | { 42 | return $this->belongsTo(Agent::class); 43 | } 44 | 45 | /** 46 | * Getting User Model 47 | * 48 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 49 | */ 50 | public function user() 51 | { 52 | return $this->morphTo(); 53 | } 54 | 55 | /** 56 | * Refresh LAST-ACTION-AT 57 | * 58 | * @return boolean 59 | */ 60 | public function refresh() 61 | { 62 | return $this->update(['last_action_at' => Carbon::now()]); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/Models/UserPasswordHistory.php: -------------------------------------------------------------------------------- 1 | 20 | */ 21 | class UserPasswordHistory extends Model 22 | { 23 | /** 24 | * The table associated with the model. 25 | * 26 | * @var string 27 | */ 28 | protected $table = 'user_password_history'; 29 | 30 | /** 31 | * The attributes that are mass assignable. 32 | * 33 | * @var array 34 | */ 35 | protected $fillable = ['pass_from', 'pass_to', 'agent_id', 'ip', 'user_id', 'user_type']; 36 | 37 | /** 38 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo 39 | */ 40 | public function agent() 41 | { 42 | return $this->belongsTo(Agent::class); 43 | } 44 | 45 | /** 46 | * Getting User Model 47 | * 48 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 49 | */ 50 | public function user() 51 | { 52 | return $this->morphTo(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/Traits/Actions/Bookmark/isBookmaker.php: -------------------------------------------------------------------------------- 1 | bookmarks()->delete(); 19 | }); 20 | } 21 | 22 | /** 23 | * Get Bookmark Object 24 | * 25 | * @param Model $bookmarkable 26 | * @return Bookmark|null 27 | */ 28 | public function getBookmark(Model $bookmarkable) 29 | { 30 | return $this->bookmarks()->where(['bookmarkable_id' => $bookmarkable->getKey(), 'bookmarkable_type' => get_class($bookmarkable)])->first(); 31 | } 32 | 33 | /** 34 | * Bookmark Model 35 | * 36 | * @param Model $bookmarkable 37 | * @param array|null $data 38 | * @return boolean 39 | */ 40 | public function bookmark(Model $bookmarkable, array $data = null) 41 | { 42 | if ($bookmark = $this->getBookmark($bookmarkable)) { 43 | return $bookmark->update(['data' => $data]); 44 | } else { 45 | return $this->bookmarks()->make(['data' => $data])->bookmarkable()->associate($bookmarkable)->save(); 46 | } 47 | } 48 | 49 | /** 50 | * Unbookmark Model 51 | * 52 | * @param Model $bookmarkable 53 | * @return boolean 54 | */ 55 | public function unbookmark(Model $bookmarkable) 56 | { 57 | if ($bookmark = $this->getBookmark($bookmarkable)) 58 | return $bookmark->delete(); 59 | 60 | return false; 61 | } 62 | 63 | /** 64 | * Has Bookmarked By This User 65 | * 66 | * @param Model $bookmarkable 67 | * @return boolean 68 | */ 69 | public function bookmarked(Model $bookmarkable) 70 | { 71 | return $this->bookmarks()->where(['bookmarkable_id' => $bookmarkable->getKey(), 'bookmarkable_type' => get_class($bookmarkable)])->exists(); 72 | } 73 | 74 | /** 75 | * Bookmarks List 76 | * 77 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 78 | */ 79 | public function bookmarks() 80 | { 81 | return $this->morphMany(Bookmark::class, 'bookmarker'); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/Traits/Actions/Bookmark/isBookmarkable.php: -------------------------------------------------------------------------------- 1 | bookmarks()->delete(); 19 | }); 20 | } 21 | 22 | /** 23 | * Get Bookmark Object 24 | * 25 | * @param Authenticatable $bookmarker 26 | * @return Bookmark|null 27 | */ 28 | public function getBookmark(Authenticatable $bookmarker) 29 | { 30 | return $this->bookmarks()->where(['bookmarker_id' => $bookmarker->getKey(), 'bookmarker_type' => get_class($bookmarker)])->first(); 31 | } 32 | 33 | /** 34 | * Bookmark with a Model 35 | * 36 | * @param Authenticatable $bookmarker 37 | * @param array|null $data 38 | * @return boolean 39 | */ 40 | public function bookmarkBy(Authenticatable $bookmarker, array $data = null) 41 | { 42 | if ($bookmark = $this->getBookmark($bookmarker)) { 43 | return $bookmark->update(['data' => $data]); 44 | } else { 45 | return $this->bookmarks()->make(['data' => $data])->bookmarker()->associate($bookmarker)->save(); 46 | } 47 | } 48 | 49 | /** 50 | * Un-bookmark with a Model 51 | * 52 | * @param Authenticatable $bookmarker 53 | * @return boolean 54 | */ 55 | public function unBookmarkBy(Authenticatable $bookmarker) 56 | { 57 | if ($bookmark = $this->getBookmark($bookmarker)) 58 | return $bookmark->delete(); 59 | 60 | return false; 61 | } 62 | 63 | /** 64 | * Has Bookmarked By Bookmarker 65 | * 66 | * @param Authenticatable $bookmarker 67 | * @return boolean 68 | */ 69 | public function bookmarkedBy(Authenticatable $bookmarker) 70 | { 71 | return $this->bookmarks()->where(['bookmarker_id' => $bookmarker->getKey(), 'bookmarker_type' => get_class($bookmarker)])->exists(); 72 | } 73 | 74 | /** 75 | * Bookmarks List 76 | * 77 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 78 | */ 79 | public function bookmarks() 80 | { 81 | return $this->morphMany(Bookmark::class, 'bookmarkable'); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/Traits/Actions/Comment/isCommentable.php: -------------------------------------------------------------------------------- 1 | comments()->delete(); 20 | } else { 21 | $model->comments()->forceDelete(); 22 | } 23 | }); 24 | } 25 | 26 | /** 27 | * Comment with a Model 28 | * 29 | * @param Authenticatable $commenter 30 | * @param string $comment 31 | * @return boolean 32 | */ 33 | public function commentBy(Authenticatable $commenter, string $comment) 34 | { 35 | return $this->comments()->make(['comment' => $comment])->commenter()->associate($commenter)->save(); 36 | } 37 | 38 | /** 39 | * Has Commented By Commenter 40 | * 41 | * @param Authenticatable $commenter 42 | * @return boolean 43 | */ 44 | public function commentedBy(Authenticatable $commenter) 45 | { 46 | return $this->comments()->where(['commenter_id' => $commenter->getKey(), 'commenter_type' => get_class($commenter)])->exists(); 47 | } 48 | 49 | /** 50 | * Comments List 51 | * 52 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 53 | */ 54 | public function comments() 55 | { 56 | return $this->morphMany(Comment::class, 'commentable'); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/Traits/Actions/Comment/isCommenter.php: -------------------------------------------------------------------------------- 1 | comments()->delete(); 20 | } else { 21 | $model->comments()->forceDelete(); 22 | } 23 | }); 24 | } 25 | 26 | /** 27 | * Comment Model 28 | * 29 | * @param Model $commentable 30 | * @param string $comment 31 | * @return boolean 32 | */ 33 | public function comment(Model $commentable, string $comment) 34 | { 35 | return $this->comments()->make(['comment' => $comment])->commentable()->associate($commentable)->save(); 36 | } 37 | 38 | /** 39 | * Has Commented By Commenter 40 | * 41 | * @param Model $commentable 42 | * @return boolean 43 | */ 44 | public function commented(Model $commentable) 45 | { 46 | return $this->comments()->where(['commentable_id' => $commentable->getKey(), 'commentable_type' => get_class($commentable)])->exists(); 47 | } 48 | 49 | /** 50 | * Comments List 51 | * 52 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 53 | */ 54 | public function comments() 55 | { 56 | return $this->morphMany(Comment::class, 'commenter'); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/Traits/Actions/Favourite/isFavorer.php: -------------------------------------------------------------------------------- 1 | favourites()->delete(); 19 | }); 20 | } 21 | 22 | /** 23 | * Favourite Model 24 | * 25 | * @param Model $favourable 26 | * @return boolean 27 | */ 28 | public function favourite(Model $favourable) 29 | { 30 | if ($this->favoured($favourable)) 31 | return true; 32 | 33 | return $this->favourites()->make()->favourable()->associate($favourable)->save(); 34 | } 35 | 36 | /** 37 | * Unfavourite Model 38 | * 39 | * @param Model $favourable 40 | * @return boolean 41 | */ 42 | public function unfavourite(Model $favourable) 43 | { 44 | if ($favourite = $this->favourites()->where(['favourable_id' => $favourable->getKey(), 'favourable_type' => get_class($favourable)])->first()) 45 | return $favourite->delete(); 46 | 47 | return false; 48 | } 49 | 50 | /** 51 | * Has Favoured By Favour 52 | * 53 | * @param Model $favourable 54 | * @return boolean 55 | */ 56 | public function favoured(Model $favourable) 57 | { 58 | return $this->favourites()->where(['favourable_id' => $favourable->getKey(), 'favourable_type' => get_class($favourable)])->exists(); 59 | } 60 | 61 | /** 62 | * Favourites List 63 | * 64 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 65 | */ 66 | public function favourites() 67 | { 68 | return $this->morphMany(Favourite::class, 'favorer'); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/Traits/Actions/Favourite/isFavourable.php: -------------------------------------------------------------------------------- 1 | favourites()->delete(); 19 | }); 20 | } 21 | 22 | /** 23 | * Favourite with a Model 24 | * 25 | * @param Authenticatable $favorer 26 | * @return boolean 27 | */ 28 | public function favouriteBy(Authenticatable $favorer) 29 | { 30 | if ($this->favouredBy($favorer)) 31 | return true; 32 | 33 | return $this->favourites()->make()->favorer()->associate($favorer)->save(); 34 | } 35 | 36 | /** 37 | * Unfavourite with a Model 38 | * 39 | * @param Authenticatable $favorer 40 | * @return boolean 41 | */ 42 | public function unFavouriteBy(Authenticatable $favorer) 43 | { 44 | if ($favourite = $this->favourites()->where(['favorer_id' => $favorer->getKey(), 'favorer_type' => get_class($favorer)])->first()) 45 | return $favourite->delete(); 46 | 47 | return false; 48 | } 49 | 50 | /** 51 | * Has Favoured By favour 52 | * 53 | * @param Authenticatable $favorer 54 | * @return boolean 55 | */ 56 | public function favouredBy(Authenticatable $favorer) 57 | { 58 | return $this->favourites()->where(['favorer_id' => $favorer->getKey(), 'favorer_type' => get_class($favorer)])->exists(); 59 | } 60 | 61 | /** 62 | * Favourites List 63 | * 64 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 65 | */ 66 | public function favourites() 67 | { 68 | return $this->morphMany(Favourite::class, 'favourable'); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/Traits/Actions/Follow/isFollowable.php: -------------------------------------------------------------------------------- 1 | follows()->delete(); 19 | }); 20 | } 21 | 22 | /** 23 | * Follow with a Model 24 | * 25 | * @param Authenticatable follower 26 | * @return boolean 27 | */ 28 | public function followBy(Authenticatable $follower) 29 | { 30 | if ($this->followedBy($follower)) 31 | return true; 32 | 33 | return $this->follows()->make()->follower()->associate($follower)->save(); 34 | } 35 | 36 | /** 37 | * Un-follow with a Model 38 | * 39 | * @param Authenticatable follower 40 | * @return boolean 41 | */ 42 | public function unFollowBy(Authenticatable $follower) 43 | { 44 | if ($follow = $this->follows()->where(['follower_id' => $follower->getKey(), 'follower_type' => get_class($follower)])->first()) 45 | return $follow->delete(); 46 | 47 | return false; 48 | } 49 | 50 | /** 51 | * Has Followed By Follower 52 | * 53 | * @param Authenticatable follower 54 | * @return boolean 55 | */ 56 | public function followedBy(Authenticatable $follower) 57 | { 58 | return $this->follows()->where(['follower_id' => $follower->getKey(), 'follower_type' => get_class($follower)])->exists(); 59 | } 60 | 61 | /** 62 | * Follows List 63 | * 64 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 65 | */ 66 | public function follows() 67 | { 68 | return $this->morphMany(Follow::class, 'followable'); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/Traits/Actions/Follow/isFollower.php: -------------------------------------------------------------------------------- 1 | follows()->delete(); 19 | }); 20 | } 21 | 22 | /** 23 | * Follow Model 24 | * 25 | * @param Model $followable 26 | * @return boolean 27 | */ 28 | public function follow(Model $followable) 29 | { 30 | if ($this->followed($followable)) 31 | return true; 32 | 33 | return $this->follows()->make()->followable()->associate($followable)->save(); 34 | } 35 | 36 | /** 37 | * Unfollow Model 38 | * 39 | * @param Model $followable 40 | * @return boolean 41 | */ 42 | public function unfollow(Model $followable) 43 | { 44 | if ($follow = $this->follows()->where(['followable_id' => $followable->getKey(), 'followable_type' => get_class($followable)])->first()) 45 | return $follow->delete(); 46 | 47 | return false; 48 | } 49 | 50 | /** 51 | * Has Followd By follower 52 | * 53 | * @param Model $followable 54 | * @return boolean 55 | */ 56 | public function followed(Model $followable) 57 | { 58 | return $this->follows()->where(['followable_id' => $followable->getKey(), 'followable_type' => get_class($followable)])->exists(); 59 | } 60 | 61 | /** 62 | * Follows List 63 | * 64 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 65 | */ 66 | public function follows() 67 | { 68 | return $this->morphMany(Follow::class, 'follower'); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/Traits/Actions/Like/isLikeable.php: -------------------------------------------------------------------------------- 1 | likes()->delete(); 19 | }); 20 | } 21 | 22 | /** 23 | * Like with a Model 24 | * 25 | * @param Authenticatable $liker 26 | * @return boolean 27 | */ 28 | public function likeBy(Authenticatable $liker) 29 | { 30 | if ($this->likedBy($liker)) 31 | return true; 32 | 33 | return $this->likes()->make()->liker()->associate($liker)->save(); 34 | } 35 | 36 | /** 37 | * Unlike with a Model 38 | * 39 | * @param Authenticatable $liker 40 | * @return boolean 41 | */ 42 | public function unLikeBy(Authenticatable $liker) 43 | { 44 | if ($like = $this->likes()->where(['liker_id' => $liker->getKey(), 'liker_type' => get_class($liker)])->first()) 45 | return $like->delete(); 46 | 47 | return false; 48 | } 49 | 50 | /** 51 | * Has Liked By Liker 52 | * 53 | * @param Authenticatable $liker 54 | * @return boolean 55 | */ 56 | public function likedBy(Authenticatable $liker) 57 | { 58 | return $this->likes()->where(['liker_id' => $liker->getKey(), 'liker_type' => get_class($liker)])->exists(); 59 | } 60 | 61 | /** 62 | * Likes List 63 | * 64 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 65 | */ 66 | public function likes() 67 | { 68 | return $this->morphMany(Like::class, 'likeable'); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/Traits/Actions/Like/isLiker.php: -------------------------------------------------------------------------------- 1 | likes()->delete(); 19 | }); 20 | } 21 | 22 | /** 23 | * Like Model 24 | * 25 | * @param Model $likeable 26 | * @return boolean 27 | */ 28 | public function like(Model $likeable) 29 | { 30 | if ($this->liked($likeable)) 31 | return true; 32 | 33 | return $this->likes()->make()->likeable()->associate($likeable)->save(); 34 | } 35 | 36 | /** 37 | * Unlike Model 38 | * 39 | * @param Model $likeable 40 | * @return boolean 41 | */ 42 | public function unlike(Model $likeable) 43 | { 44 | if ($like = $this->likes()->where(['likeable_id' => $likeable->getKey(), 'likeable_type' => get_class($likeable)])->first()) 45 | return $like->delete(); 46 | 47 | return false; 48 | } 49 | 50 | /** 51 | * Has Liked By Liker 52 | * 53 | * @param Model $likeable 54 | * @return boolean 55 | */ 56 | public function liked(Model $likeable) 57 | { 58 | return $this->likes()->where(['likeable_id' => $likeable->getKey(), 'likeable_type' => get_class($likeable)])->exists(); 59 | } 60 | 61 | /** 62 | * liker List 63 | * 64 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 65 | */ 66 | public function likes() 67 | { 68 | return $this->morphMany(Like::class, 'liker'); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/Traits/Actions/Review/isReviewable.php: -------------------------------------------------------------------------------- 1 | reviews()->delete(); 19 | }); 20 | } 21 | 22 | /** 23 | * Get Review Object 24 | * 25 | * @param Authenticatable $reviewer 26 | * @return Vote|null 27 | */ 28 | public function getReview(Authenticatable $reviewer) 29 | { 30 | return $this->reviews()->where(['reviewer_id' => $reviewer->getKey(), 'reviewer_type' => get_class($reviewer)])->first(); 31 | } 32 | 33 | /** 34 | * Review Model By reviewer 35 | * 36 | * @param Authenticatable $reviewer 37 | * @param float $rate 38 | * @param string|null $comment 39 | * @return boolean 40 | */ 41 | public function reviewBy(Authenticatable $reviewer, float $rate = 0, string $comment = null) 42 | { 43 | if ($review = $this->getReview($reviewer)) { 44 | return $review->update([ 45 | 'rate' => $rate, 46 | 'comment' => $comment 47 | ]); 48 | } else { 49 | return $this->reviews()->make([ 50 | 'rate' => $rate, 51 | 'comment' => $comment 52 | ])->reviewer()->associate($reviewer)->save(); 53 | } 54 | } 55 | 56 | /** 57 | * UnReview Model By reviewer 58 | * 59 | * @param Authenticatable $reviewer 60 | * @return boolean 61 | */ 62 | public function unReviewBy(Authenticatable $reviewer) 63 | { 64 | if ($review = $this->getReview($reviewer)) 65 | return $review->delete(); 66 | 67 | return false; 68 | } 69 | 70 | /** 71 | * Has Reviewed By reviewer 72 | * 73 | * @param Authenticatable $reviewer 74 | * @return boolean 75 | */ 76 | public function reviewedBy(Authenticatable $reviewer) 77 | { 78 | return $this->reviews()->where(['reviewer_id' => $reviewer->getKey(), 'reviewer_type' => get_class($reviewer)])->exists(); 79 | } 80 | 81 | /** 82 | * Reviews List 83 | * 84 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 85 | */ 86 | public function reviews() 87 | { 88 | return $this->morphMany(Review::class, 'reviewable'); 89 | } 90 | 91 | /** 92 | * Getting rating value 93 | * 94 | * @return float 95 | */ 96 | public function getRatingAttribute() 97 | { 98 | if ($this->relationLoaded('reviews')) 99 | return (float) $this->reviews->avg('rate'); 100 | 101 | return (float) $this->reviews()->avg('rate'); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /src/Traits/Actions/Review/isReviewer.php: -------------------------------------------------------------------------------- 1 | reviews()->delete(); 19 | }); 20 | } 21 | 22 | /** 23 | * Get Review Object 24 | * 25 | * @param Model $reviewable 26 | * @return Vote|null 27 | */ 28 | public function getReview(Model $reviewable) 29 | { 30 | return $this->reviews()->where(['reviewable_id' => $reviewable->getKey(), 'reviewable_type' => get_class($reviewable)])->first(); 31 | } 32 | 33 | /** 34 | * Review Model 35 | * 36 | * @param Model $reviewable 37 | * @param float $rate 38 | * @param string|null $comment 39 | * @return boolean 40 | */ 41 | public function review(Model $reviewable, float $rate = 0, string $comment = null) 42 | { 43 | if ($review = $this->getReview($reviewable)) { 44 | return $review->update([ 45 | 'rate' => $rate, 46 | 'comment' => $comment 47 | ]); 48 | } else { 49 | return $this->reviews()->make([ 50 | 'rate' => $rate, 51 | 'comment' => $comment, 52 | ])->reviewable()->associate($reviewable)->save(); 53 | } 54 | } 55 | 56 | /** 57 | * UnReview Model 58 | * 59 | * @param Model $reviewable 60 | * @return boolean 61 | */ 62 | public function unReview(Model $reviewable) 63 | { 64 | if ($review = $this->getReview($reviewable)) 65 | return $review->delete(); 66 | 67 | return false; 68 | } 69 | 70 | /** 71 | * Has Reviewed By Reviewer 72 | * 73 | * @param Model $reviewable 74 | * @return boolean 75 | */ 76 | public function reviewed(Model $reviewable) 77 | { 78 | return $this->reviews()->where(['reviewable_id' => $reviewable->getKey(), 'reviewable_type' => get_class($reviewable)])->exists(); 79 | } 80 | 81 | /** 82 | * Reviews List 83 | * 84 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 85 | */ 86 | public function reviews() 87 | { 88 | return $this->morphMany(Review::class, 'reviewer'); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/Traits/Actions/Subscribe/isSubscribable.php: -------------------------------------------------------------------------------- 1 | subscriptions()->delete(); 19 | }); 20 | } 21 | 22 | /** 23 | * Subscribe with a Model 24 | * 25 | * @param Authenticatable $subscriber 26 | * @return boolean 27 | */ 28 | public function subscribeBy(Authenticatable $subscriber) 29 | { 30 | if ($this->subscribedBy($subscriber)) 31 | return true; 32 | 33 | return $this->subscriptions()->make()->subscriber()->associate($subscriber)->save(); 34 | } 35 | 36 | /** 37 | * Unsubscribe with a Model 38 | * 39 | * @param Authenticatable $subscriber 40 | * @return boolean 41 | */ 42 | public function unSubscribeBy(Authenticatable $subscriber) 43 | { 44 | if ($subscription = $this->subscriptions()->where(['subscriber_id' => $subscriber->getKey(), 'subscriber_type' => get_class($subscriber)])->first()) 45 | return $subscription->delete(); 46 | 47 | return false; 48 | } 49 | 50 | /** 51 | * Has Subscribed By Subscriber 52 | * 53 | * @param Authenticatable $subscriber 54 | * @return boolean 55 | */ 56 | public function subscribedBy(Authenticatable $subscriber) 57 | { 58 | return $this->subscriptions()->where(['subscriber_id' => $subscriber->getKey(), 'subscriber_type' => get_class($subscriber)])->exists(); 59 | } 60 | 61 | /** 62 | * Subscriptions List 63 | * 64 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 65 | */ 66 | public function subscriptions() 67 | { 68 | return $this->morphMany(Subscribe::class, 'subscribable'); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/Traits/Actions/Subscribe/isSubscriber.php: -------------------------------------------------------------------------------- 1 | subscriptions()->delete(); 19 | }); 20 | } 21 | 22 | /** 23 | * Subscribe Model 24 | * 25 | * @param Model $subscribable 26 | * @return boolean 27 | */ 28 | public function subscribe(Model $subscribable) 29 | { 30 | if ($this->subscribed($subscribable)) 31 | return true; 32 | 33 | return $this->subscriptions()->make()->subscribable()->associate($subscribable)->save(); 34 | } 35 | 36 | /** 37 | * Unsubscribe Model 38 | * 39 | * @param Model $subscribable 40 | * @return boolean 41 | */ 42 | public function unsubscribe(Model $subscribable) 43 | { 44 | if ($subscription = $this->subscriptions()->where(['subscribable_id' => $subscribable->getKey(), 'subscribable_type' => get_class($subscribable)])->first()) 45 | return $subscription->delete(); 46 | 47 | return false; 48 | } 49 | 50 | /** 51 | * Has Subscribed By Subscriber 52 | * 53 | * @param Model $subscribable 54 | * @return boolean 55 | */ 56 | public function subscribed(Model $subscribable) 57 | { 58 | return $this->subscriptions()->where(['subscribable_id' => $subscribable->getKey(), 'subscribable_type' => get_class($subscribable)])->exists(); 59 | } 60 | 61 | /** 62 | * Subscriptions List 63 | * 64 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 65 | */ 66 | public function subscriptions() 67 | { 68 | return $this->morphMany(Subscribe::class, 'subscriber'); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/Traits/Actions/Vote/isVotable.php: -------------------------------------------------------------------------------- 1 | votes()->delete(); 19 | }); 20 | } 21 | 22 | /** 23 | * Get Vote Object 24 | * 25 | * @param Authenticatable $voter 26 | * @return Vote|null 27 | */ 28 | public function getVote(Authenticatable $voter) 29 | { 30 | return $this->votes()->where(['voter_id' => $voter->getKey(), 'voter_type' => get_class($voter)])->first(); 31 | } 32 | 33 | /** 34 | * VoteUp with a Model 35 | * 36 | * @param Authenticatable $voter 37 | * @return boolean 38 | */ 39 | public function voteUpBy(Authenticatable $voter) 40 | { 41 | if ($vote = $this->getVote($voter)) { 42 | return $vote->update(['vote' => true]); 43 | } else { 44 | return $this->votes()->make(['vote' => true])->voter()->associate($voter)->save(); 45 | } 46 | } 47 | 48 | /** 49 | * VoteDown with a Model 50 | * 51 | * @param Authenticatable $voter 52 | * @return boolean 53 | */ 54 | public function voteDownBy(Authenticatable $voter) 55 | { 56 | if ($vote = $this->getVote($voter)) { 57 | return $vote->update(['vote' => false]); 58 | } else { 59 | return $this->votes()->make(['vote' => false])->voter()->associate($voter)->save(); 60 | } 61 | } 62 | 63 | /** 64 | * Unvote with a Model 65 | * 66 | * @param Authenticatable $voter 67 | * @return boolean 68 | */ 69 | public function unVoteBy(Authenticatable $voter) 70 | { 71 | if ($vote = $this->getVote($voter)) 72 | return $vote->delete(); 73 | 74 | return false; 75 | } 76 | 77 | /** 78 | * Has Voted By Voter 79 | * 80 | * @param Authenticatable $voter 81 | * @return boolean 82 | */ 83 | public function votedBy(Authenticatable $voter) 84 | { 85 | return $this->votes()->where(['voter_id' => $voter->getKey(), 'voter_type' => get_class($voter)])->exists(); 86 | } 87 | 88 | /** 89 | * Votes List 90 | * 91 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 92 | */ 93 | public function votes() 94 | { 95 | return $this->morphMany(Vote::class, 'votable'); 96 | } 97 | 98 | /** 99 | * Votes Up List 100 | * 101 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 102 | */ 103 | public function upVotes() 104 | { 105 | return $this->votes()->where('vote', true); 106 | } 107 | 108 | /** 109 | * Votes Down List 110 | * 111 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 112 | */ 113 | public function downVotes() 114 | { 115 | return $this->votes()->where('vote', false); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/Traits/Actions/Vote/isVoter.php: -------------------------------------------------------------------------------- 1 | votes()->delete(); 19 | }); 20 | } 21 | 22 | /** 23 | * Get Vote Object 24 | * 25 | * @param Model $votable 26 | * @return Vote|null 27 | */ 28 | public function getVote(Model $votable) 29 | { 30 | return $this->votes()->where(['votable_id' => $votable->getKey(), 'votable_type' => get_class($votable)])->first(); 31 | } 32 | 33 | /** 34 | * VoteUp Model 35 | * 36 | * @param Model $votable 37 | * @return boolean 38 | */ 39 | public function voteUp(Model $votable) 40 | { 41 | if ($vote = $this->getVote($votable)) { 42 | return $vote->update(['vote' => true]); 43 | } else { 44 | return $this->votes()->make(['vote' => true])->votable()->associate($votable)->save(); 45 | } 46 | } 47 | 48 | /** 49 | * VoteDown Model 50 | * 51 | * @param Model $votable 52 | * @return boolean 53 | */ 54 | public function voteDown(Model $votable) 55 | { 56 | if ($vote = $this->getVote($votable)) { 57 | return $vote->update(['vote' => false]); 58 | } else { 59 | return $this->votes()->make(['vote' => false])->votable()->associate($votable)->save(); 60 | } 61 | } 62 | 63 | /** 64 | * Unvote Model 65 | * 66 | * @param Model $votable 67 | * @return boolean 68 | */ 69 | public function unvote(Model $votable) 70 | { 71 | if ($vote = $this->getVote($votable)) 72 | return $vote->delete(); 73 | 74 | return false; 75 | } 76 | 77 | /** 78 | * Has Voted By Voter 79 | * 80 | * @param Model $votable 81 | * @return boolean 82 | */ 83 | public function voted(Model $votable) 84 | { 85 | return $this->votes()->where(['votable_id' => $votable->getKey(), 'votable_type' => get_class($votable)])->exists(); 86 | } 87 | 88 | /** 89 | * Votes List 90 | * 91 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 92 | */ 93 | public function votes() 94 | { 95 | return $this->morphMany(Vote::class, 'voter'); 96 | } 97 | 98 | /** 99 | * Votes Up List 100 | * 101 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 102 | */ 103 | public function upVotes() 104 | { 105 | return $this->votes()->where('vote', true); 106 | } 107 | 108 | /** 109 | * Votes Down List 110 | * 111 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 112 | */ 113 | public function downVotes() 114 | { 115 | return $this->votes()->where('vote', false); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/Traits/HasDevices.php: -------------------------------------------------------------------------------- 1 | 14 | */ 15 | trait HasDevices 16 | { 17 | /** 18 | * Get all deivces list. 19 | * 20 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 21 | */ 22 | public function devices() 23 | { 24 | return $this->morphMany(UserAgent::class, 'user')->with(['agent.browser', 'agent.device', 'agent.operationSystem']); 25 | } 26 | 27 | /** 28 | * List of FCM token. 29 | * 30 | * @return array|null 31 | */ 32 | public function getFcmListArrtibute() 33 | { 34 | $list = $this->devices()->whereNotNull('fcm_token')->get(); 35 | if ($list->isEmpty()) return null; 36 | 37 | return $list->pluck('fcm_token')->toArray(); 38 | } 39 | 40 | public function getCurrentDeviceSignatureAttribute() 41 | { 42 | return session()->isStarted() ? session()->get('device-signature') : request()->headers->get('device-signature'); 43 | } 44 | 45 | /** 46 | * Check if device detected 47 | * 48 | * @param string|null $signature 49 | * @return boolean 50 | */ 51 | public function hasDetectedDevice(string $signature = null) 52 | { 53 | if (!$signature && !($signature = $this->currentDeviceSignature)) 54 | return false; 55 | 56 | return $this->devices()->where([ 57 | 'agent_id' => agent()->id, 58 | 'signature' => $signature 59 | ])->exists(); 60 | } 61 | 62 | /** 63 | * Add Current Agent To Current User 64 | * 65 | * @param string|null $fcm 66 | * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder 67 | */ 68 | public function detectDevice(string $fcm = null) 69 | { 70 | if (!($signature = $this->currentDeviceSignature)) { 71 | $signature = Str::uuid() . '-' . Str::random(); 72 | 73 | if (session()->isStarted()) 74 | session()->put('device-signature', $signature); 75 | } 76 | 77 | return $this->devices()->updateOrCreate([ 78 | 'agent_id' => agent()->id, 79 | 'signature' => $signature 80 | ], [ 81 | 'fcm_token' => $fcm, 82 | 'ip' => agent()->ip, 83 | 'last_action_at' => Carbon::now() 84 | ]); 85 | } 86 | 87 | /** 88 | * Remove Device by signature 89 | * 90 | * @param string $signature 91 | * @return bool 92 | */ 93 | public function removeDevice(string $signature) 94 | { 95 | if ($this->devices()->where('signature', $signature)->delete() == 0) 96 | return false; 97 | 98 | if ($this->currentDeviceSignature == $signature) { 99 | if (session()->isStarted()) 100 | session()->forget('device-signature'); 101 | } 102 | 103 | return true; 104 | } 105 | 106 | /** 107 | * Remove All Device. 108 | * 109 | * @return bool 110 | */ 111 | public function removeAllDevices() 112 | { 113 | if (session()->isStarted()) 114 | session()->forget('device-signature'); 115 | 116 | return $this->devices()->delete() > 0; 117 | } 118 | 119 | /** 120 | * Getting current device 121 | * 122 | * @return UserAgent|null 123 | */ 124 | public function getCurrentDeviceAttribute() 125 | { 126 | if (!($signature = $this->currentDeviceSignature)) 127 | return null; 128 | 129 | return $this->devices()->where('signature', $signature)->with(['agent.operationSystem', 'agent.browser', 'agent.device'])->first(); 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /src/Traits/HasPermissions.php: -------------------------------------------------------------------------------- 1 | 14 | */ 15 | trait HasPermissions 16 | { 17 | protected $permissionsListArray; 18 | 19 | protected static function bootHasPermissions() 20 | { 21 | // Deleting 22 | self::deleting(function ($model) { 23 | $model->permissibles()->delete(); 24 | }); 25 | } 26 | 27 | /** 28 | * Get all of the main permissibles objects. 29 | */ 30 | public function permissibles() 31 | { 32 | return $this->morphMany(Permissible::class, 'permissible'); 33 | } 34 | 35 | /** 36 | * Get all attached permissions to the model. 37 | * 38 | * @return \Illuminate\Database\Eloquent\Relations\MorphToMany 39 | */ 40 | public function permissions() 41 | { 42 | return $this->morphToMany(Permission::class, 'permissible', 'permissibles', 'permissible_id', 'permission_id'); 43 | } 44 | 45 | /** 46 | * Getting Permissoins List 47 | * 48 | * @return array 49 | */ 50 | public function getPermissionsListAttribute() 51 | { 52 | if (is_array($this->permissionsListArray)) return $this->permissionsListArray; 53 | 54 | $permissions = $this->permissions()->pluck('permissions.code')->toArray(); 55 | 56 | // Append Roles Permissions IF Found 57 | if (!in_array(HasRoles::class, class_uses($this))) return $permissions; 58 | 59 | $permissions = array_merge( 60 | $permissions, 61 | Permissible::where('permissible_type', Role::class) 62 | ->whereIn('permissible_id', $this->roles()->pluck('roles.id')->toArray()) 63 | ->join('permissions', 'permissions.id', '=', 'permissibles.permission_id') 64 | ->pluck('permissions.code')->toArray() 65 | ); 66 | 67 | return $this->permissionsListArray = $permissions; 68 | } 69 | 70 | /** 71 | * Prepare Permissions IDs 72 | * 73 | * @param array $permissions 74 | * @return array 75 | */ 76 | private function preparePermissionsIds(array $permissions) 77 | { 78 | return Permission::whereIn('code', $this->preparePermissionsArray($permissions))->get()->pluck('id')->toArray(); 79 | } 80 | 81 | /** 82 | * Prepare Permissions for checkers 83 | * 84 | * @param array $permissions 85 | * @return array 86 | */ 87 | private function preparePermissionsArray($permissions) 88 | { 89 | if (is_array($permissions[0])) $permissions = $permissions[0]; 90 | if (is_string($permissions)) $permissions = explode(',', $permissions); 91 | 92 | return $permissions; 93 | } 94 | 95 | /** 96 | * Give model permissions 97 | * 98 | * @param string ...$permissions 99 | * @return boolean 100 | */ 101 | public function permit(...$permissions) 102 | { 103 | if (!empty(array_filter($this->permissions()->sync($this->preparePermissionsIds($permissions), false)))) { 104 | $this->permissionsListArray = null; 105 | return true; 106 | } 107 | 108 | return false; 109 | } 110 | 111 | /** 112 | * Checking if all permissions allowed 113 | * 114 | * @param string $permissions 115 | * @return boolean 116 | */ 117 | public function permitted(...$permissions) 118 | { 119 | if (empty($permissions)) return; 120 | 121 | $permissions = $this->preparePermissionsArray($permissions); 122 | $permissionsList = $this->permissionsList; 123 | 124 | foreach ($permissions as $permission) 125 | if (!in_array($permission, $permissionsList)) 126 | return false; 127 | 128 | return true; 129 | } 130 | 131 | /** 132 | * Checking if any of permissions allowed 133 | * 134 | * @param string $permissions 135 | * @return boolean 136 | */ 137 | public function permittedAny(...$permissions) 138 | { 139 | if (empty($permissions)) return; 140 | 141 | $permissions = $this->preparePermissionsArray($permissions); 142 | $permissionsList = $this->permissionsList; 143 | 144 | foreach ($permissions as $permission) 145 | if (in_array($permission, $permissionsList)) 146 | return true; 147 | 148 | return false; 149 | } 150 | 151 | /** 152 | * Forbid model Permissions 153 | * 154 | * @param string ...$permissions 155 | * @return boolean 156 | */ 157 | public function forbid(...$permissions) 158 | { 159 | if ($this->permissions()->detach($this->preparePermissionsIds($permissions)) > 0) { 160 | $this->permissionsListArray = null; 161 | return true; 162 | } 163 | 164 | return false; 165 | } 166 | 167 | /** 168 | * Checking if all permissions disallowed 169 | * 170 | * @param string $permissions 171 | * @return boolean 172 | */ 173 | public function forbad(...$permissions) 174 | { 175 | if (empty($permissions)) return; 176 | 177 | $permissions = $this->preparePermissionsArray($permissions); 178 | $permissionsList = $this->permissionsList; 179 | 180 | foreach ($permissions as $permission) 181 | if (in_array($permission, $permissionsList)) 182 | return false; 183 | 184 | return true; 185 | } 186 | 187 | /** 188 | * Checking if any of permissions disallowed 189 | * 190 | * @param string $permissions 191 | * @return boolean 192 | */ 193 | public function forbadAny(...$permissions) 194 | { 195 | if (empty($permissions)) return; 196 | 197 | $permissions = $this->preparePermissionsArray($permissions); 198 | $permissionsList = $this->permissionsList; 199 | 200 | foreach ($permissions as $permission) 201 | if (!in_array($permission, $permissionsList)) 202 | return true; 203 | 204 | return false; 205 | } 206 | 207 | /** 208 | * Sync the model Permissions 209 | * 210 | * @param string ...$permissions 211 | * @return boolean 212 | */ 213 | public function syncPermissions(...$permissions) 214 | { 215 | if (!empty(array_filter($this->permissions()->sync($this->preparePermissionsIds($permissions))))) { 216 | $this->permissionsListArray = null; 217 | return true; 218 | } 219 | 220 | return false; 221 | } 222 | } 223 | -------------------------------------------------------------------------------- /src/Traits/HasRoles.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | trait HasRoles 14 | { 15 | protected $rolesListArray; 16 | 17 | protected static function bootHasRoles() 18 | { 19 | // Deleting 20 | self::deleting(function ($model) { 21 | $model->roleables()->delete(); 22 | }); 23 | } 24 | 25 | /** 26 | * Get all of the main roleable objects. 27 | */ 28 | public function roleables() 29 | { 30 | return $this->morphMany(Roleable::class, 'roleable'); 31 | } 32 | 33 | /** 34 | * Get all attached roles to the model. 35 | * 36 | * @return \Illuminate\Database\Eloquent\Relations\MorphToMany 37 | */ 38 | public function roles() 39 | { 40 | return $this->morphToMany(Role::class, 'roleable', 'roleables', 'roleable_id', 'role_id'); 41 | } 42 | 43 | /** 44 | * Getting Permissoins List 45 | * 46 | * @return array 47 | */ 48 | public function getRolesListAttribute() 49 | { 50 | if (is_array($this->rolesListArray)) return $this->rolesListArray; 51 | 52 | return $this->rolesListArray = $this->roles()->pluck('roles.code')->toArray(); 53 | } 54 | 55 | /** 56 | * Prepare Roles IDs 57 | * 58 | * @param array $roles 59 | * @return array 60 | */ 61 | private function prepareRolesIds($roles) 62 | { 63 | return Role::whereIn('code', $this->prepareRolesArray($roles))->get()->pluck('id')->toArray(); 64 | } 65 | 66 | /** 67 | * Prepare Roles for checkers 68 | * 69 | * @param array $roles 70 | * @return array 71 | */ 72 | private function prepareRolesArray($roles) 73 | { 74 | if (is_array($roles[0])) $roles = $roles[0]; 75 | if (is_string($roles)) $roles = explode(',', $roles); 76 | 77 | return $roles; 78 | } 79 | 80 | /** 81 | * Give model roles 82 | * 83 | * @param string ...$roles 84 | * @return boolean 85 | */ 86 | public function entrust(...$roles) 87 | { 88 | if (!empty(array_filter($this->roles()->sync($this->prepareRolesIds($roles), false)))) { 89 | $this->rolesListArray = null; 90 | return true; 91 | } 92 | 93 | return false; 94 | } 95 | 96 | /** 97 | * Checking if all roles allowed 98 | * 99 | * @param string $roles 100 | * @return boolean 101 | */ 102 | public function entrusted(...$roles) 103 | { 104 | if (empty($roles)) return; 105 | 106 | $roles = $this->prepareRolesArray($roles); 107 | $rolesList = $this->rolesList; 108 | 109 | foreach ($roles as $role) 110 | if (!in_array($role, $rolesList)) 111 | return false; 112 | 113 | return true; 114 | } 115 | 116 | /** 117 | * Checking if any of roles allowed 118 | * 119 | * @param string $roles 120 | * @return boolean 121 | */ 122 | public function entrustedAny(...$roles) 123 | { 124 | if (empty($roles)) return; 125 | 126 | $roles = $this->prepareRolesArray($roles); 127 | $rolesList = $this->rolesList; 128 | 129 | foreach ($roles as $role) 130 | if (in_array($role, $rolesList)) 131 | return true; 132 | 133 | return false; 134 | } 135 | 136 | /** 137 | * Forbid model Roles 138 | * 139 | * @param string ...$roles 140 | * @return boolean 141 | */ 142 | public function distrust(...$roles) 143 | { 144 | if ($this->roles()->detach($this->prepareRolesIds($roles)) > 1) { 145 | $this->rolesListArray = null; 146 | return true; 147 | } 148 | 149 | return false; 150 | } 151 | 152 | /** 153 | * Checking if all roles disallowed 154 | * 155 | * @param string $roles 156 | * @return boolean 157 | */ 158 | public function distrusted(...$roles) 159 | { 160 | if (empty($roles)) return; 161 | 162 | $roles = $this->prepareRolesArray($roles); 163 | $rolesList = $this->rolesList; 164 | 165 | foreach ($roles as $role) 166 | if (in_array($role, $rolesList)) 167 | return false; 168 | 169 | return true; 170 | } 171 | 172 | /** 173 | * Checking if any of roles disallowed 174 | * 175 | * @param string $roles 176 | * @return boolean 177 | */ 178 | public function distrustedAny(...$roles) 179 | { 180 | if (empty($roles)) return; 181 | 182 | $roles = $this->prepareRolesArray($roles); 183 | $rolesList = $this->rolesList; 184 | 185 | foreach ($roles as $role) 186 | if (!in_array($role, $rolesList)) 187 | return true; 188 | 189 | return false; 190 | } 191 | 192 | /** 193 | * Sync the model Roles 194 | * 195 | * @param string ...$roles 196 | * @return boolean 197 | */ 198 | public function syncRoles(...$roles) 199 | { 200 | if (!empty(array_filter($this->roles()->sync($this->prepareRolesIds($roles))))) { 201 | $this->rolesListArray = null; 202 | return true; 203 | } 204 | 205 | return false; 206 | } 207 | } 208 | -------------------------------------------------------------------------------- /src/Traits/Password/HasHashedPassword.php: -------------------------------------------------------------------------------- 1 | 11 | */ 12 | trait HasHashedPassword 13 | { 14 | /** 15 | * Set uuid key on creating 16 | * 17 | * @return void 18 | */ 19 | public static function bootHasHashedPassword() 20 | { 21 | self::creating(function ($model) { 22 | if (!empty($model->password)) 23 | $model->password = Hash::make($model->password); 24 | }); 25 | 26 | self::updating(function ($model) { 27 | if ($model->isDirty('password') && !empty($model->password)) { 28 | $model->password = Hash::make($model->password); 29 | } 30 | }); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Traits/Password/HasPasswordHistory.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | trait HasPasswordHistory 14 | { 15 | /** 16 | * Set uuid key on creating 17 | * 18 | * @return void 19 | */ 20 | public static function bootHasPasswordHistory() 21 | { 22 | self::saved(function ($model) { 23 | if ($model->isDirty('password') && !$model->wasRecentlyCreated) $model->passwordHistory()->create([ 24 | 'pass_from' => $model->getOriginal('password'), 25 | 'pass_to' => $model->password, 26 | 'ip' => agent()->ip, 27 | 'agent_id' => agent()->id 28 | ]); 29 | }); 30 | } 31 | 32 | /** 33 | * Get all user's Hashed-Passwords. 34 | * 35 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 36 | */ 37 | public function passwordHistory() 38 | { 39 | return $this->morphMany(UserPasswordHistory::class, 'user') 40 | ->with(['agent', 'agent.device', 'agent.browser', 'agent.operationSystem']); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/UsersServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 34 | // Devices 35 | __DIR__ . '/database/migrations/2021_02_01_000024_create_user_agents_table.php' => database_path('migrations/2021_02_01_000024_create_user_agents_table.php'), 36 | 37 | // Passwords 38 | __DIR__ . '/database/migrations/2021_02_01_000025_create_user_password_history_table.php' => database_path('migrations/2021_02_01_000025_create_user_password_history_table.php'), 39 | 40 | // Roles & Permissions 41 | __DIR__ . '/database/migrations/2021_02_01_000026_create_permissions_table.php' => database_path('migrations/2021_02_01_000026_create_permissions_table.php'), 42 | __DIR__ . '/database/migrations/2021_02_01_000027_create_permission_translations_table.php' => database_path('migrations/2021_02_01_000027_create_permission_translations_table.php'), 43 | __DIR__ . '/database/migrations/2021_02_01_000028_create_permissibles_table.php' => database_path('migrations/2021_02_01_000028_create_permissibles_table.php'), 44 | __DIR__ . '/database/migrations/2021_02_01_000029_create_roles_table.php' => database_path('migrations/2021_02_01_000029_create_roles_table.php'), 45 | __DIR__ . '/database/migrations/2021_02_01_000030_create_role_translations_table.php' => database_path('migrations/2021_02_01_000030_create_role_translations_table.php'), 46 | __DIR__ . '/database/migrations/2021_02_01_000031_create_roleables_table.php' => database_path('migrations/2021_02_01_000031_create_roleables_table.php'), 47 | 48 | // Actions 49 | __DIR__ . '/database/migrations/2021_02_01_000077_create_likes_table.php' => database_path('migrations/2021_02_01_000077_create_likes_table.php'), 50 | __DIR__ . '/database/migrations/2021_02_01_000078_create_follows_table.php' => database_path('migrations/2021_02_01_000078_create_follows_table.php'), 51 | __DIR__ . '/database/migrations/2021_02_01_000079_create_favourites_table.php' => database_path('migrations/2021_02_01_000079_create_favourites_table.php'), 52 | __DIR__ . '/database/migrations/2021_02_01_000080_create_bookmarks_table.php' => database_path('migrations/2021_02_01_000080_create_bookmarks_table.php'), 53 | __DIR__ . '/database/migrations/2021_02_01_000081_create_votes_table.php' => database_path('migrations/2021_02_01_000081_create_votes_table.php'), 54 | __DIR__ . '/database/migrations/2021_02_01_000082_create_reviews_table.php' => database_path('migrations/2021_02_01_000082_create_reviews_table.php'), 55 | __DIR__ . '/database/migrations/2021_02_01_000083_create_subscribes_table.php' => database_path('migrations/2021_02_01_000083_create_subscribes_table.php'), 56 | __DIR__ . '/database/migrations/2021_02_01_000084_create_comments_table.php' => database_path('migrations/2021_02_01_000084_create_comments_table.php'), 57 | 58 | ], ['pharaonic', 'laravel-users']); 59 | 60 | 61 | $this->initPermissionsAndRoles(); 62 | } 63 | 64 | /** 65 | * Set Blade directives & Router Middlewares. 66 | * 67 | * @return void 68 | */ 69 | private function initPermissionsAndRoles() 70 | { 71 | // Router 72 | $router = $this->app->make(Router::class); 73 | $router->aliasMiddleware('permitted', Permitted::class); 74 | $router->aliasMiddleware('permittedAny', PermittedAny::class); 75 | $router->aliasMiddleware('entrusted', Entrusted::class); 76 | $router->aliasMiddleware('entrustedAny', EntrustedAny::class); 77 | 78 | // Directives 79 | foreach ([ 80 | 'entrusted', 'entrustedAny', 'distrusted', 'distrustedAny', 81 | 'permitted', 'permittedAny', 'forbad', 'forbadAny' 82 | ] as $action) { 83 | Blade::if($action, function (...$list) use ($action) { 84 | return $action($list); 85 | }); 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/database/migrations/2021_02_01_000024_create_user_agents_table.php: -------------------------------------------------------------------------------- 1 | id(); 18 | $table->morphs('user'); 19 | $table->string('signature'); 20 | $table->foreignId('agent_id'); 21 | $table->unique(['user_id', 'user_type', 'signature', 'agent_id']); 22 | 23 | $table->string('ip'); 24 | $table->string('fcm_token')->nullable(); 25 | $table->timestamp('last_action_at'); 26 | $table->timestamps(); 27 | 28 | // Relationships 29 | $table->foreign('agent_id')->references('id')->on('agents')->onDelete('cascade'); 30 | }); 31 | } 32 | 33 | /** 34 | * Reverse the migrations. 35 | * 36 | * @return void 37 | */ 38 | public function down() 39 | { 40 | Schema::dropIfExists('user_agents'); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/database/migrations/2021_02_01_000025_create_user_password_history_table.php: -------------------------------------------------------------------------------- 1 | id(); 18 | $table->string('pass_from'); 19 | $table->string('pass_to'); 20 | $table->foreignId('agent_id'); 21 | $table->string('ip'); 22 | 23 | $table->morphs('user'); 24 | $table->timestamps(); 25 | 26 | // Relationships 27 | $table->foreign('agent_id')->references('id')->on('agents')->onDelete('cascade'); 28 | }); 29 | } 30 | 31 | /** 32 | * Reverse the migrations. 33 | * 34 | * @return void 35 | */ 36 | public function down() 37 | { 38 | Schema::dropIfExists('user_password_history'); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/database/migrations/2021_02_01_000026_create_permissions_table.php: -------------------------------------------------------------------------------- 1 | id(); 18 | $table->string('code', 50); 19 | $table->timestamps(); 20 | }); 21 | } 22 | 23 | /** 24 | * Reverse the migrations. 25 | * 26 | * @return void 27 | */ 28 | public function down() 29 | { 30 | Schema::dropIfExists('permissions'); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/database/migrations/2021_02_01_000027_create_permission_translations_table.php: -------------------------------------------------------------------------------- 1 | id(); 18 | $table->string('title'); 19 | 20 | $table->string('locale')->index(); 21 | $table->unsignedBigInteger('permission_id'); 22 | $table->unique(['permission_id', 'locale']); 23 | $table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade'); 24 | }); 25 | } 26 | 27 | /** 28 | * Reverse the migrations. 29 | * 30 | * @return void 31 | */ 32 | public function down() 33 | { 34 | Schema::dropIfExists('permission_translations'); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/database/migrations/2021_02_01_000028_create_permissibles_table.php: -------------------------------------------------------------------------------- 1 | id(); 18 | $table->foreignId('permission_id'); 19 | $table->morphs('permissible'); 20 | 21 | // Relationships 22 | $table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade'); 23 | }); 24 | } 25 | 26 | /** 27 | * Reverse the migrations. 28 | * 29 | * @return void 30 | */ 31 | public function down() 32 | { 33 | Schema::dropIfExists('permissibles'); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/database/migrations/2021_02_01_000029_create_roles_table.php: -------------------------------------------------------------------------------- 1 | id(); 18 | $table->string('code', 50); 19 | $table->timestamps(); 20 | }); 21 | } 22 | 23 | /** 24 | * Reverse the migrations. 25 | * 26 | * @return void 27 | */ 28 | public function down() 29 | { 30 | Schema::dropIfExists('roles'); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/database/migrations/2021_02_01_000030_create_role_translations_table.php: -------------------------------------------------------------------------------- 1 | id(); 18 | $table->string('title'); 19 | 20 | $table->string('locale')->index(); 21 | $table->unsignedBigInteger('role_id'); 22 | $table->unique(['role_id', 'locale']); 23 | $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade'); 24 | }); 25 | } 26 | 27 | /** 28 | * Reverse the migrations. 29 | * 30 | * @return void 31 | */ 32 | public function down() 33 | { 34 | Schema::dropIfExists('role_translations'); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/database/migrations/2021_02_01_000031_create_roleables_table.php: -------------------------------------------------------------------------------- 1 | id(); 18 | $table->foreignId('role_id'); 19 | $table->morphs('roleable'); 20 | 21 | // Relationships 22 | $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade'); 23 | }); 24 | } 25 | 26 | /** 27 | * Reverse the migrations. 28 | * 29 | * @return void 30 | */ 31 | public function down() 32 | { 33 | Schema::dropIfExists('roleables'); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/database/migrations/2021_02_01_000077_create_likes_table.php: -------------------------------------------------------------------------------- 1 | bigIncrements('id'); 18 | 19 | $table->string('likeable_id'); 20 | $table->string('likeable_type'); 21 | 22 | $table->string('liker_id'); 23 | $table->string('liker_type'); 24 | 25 | $table->timestamps(); 26 | }); 27 | } 28 | 29 | /** 30 | * Reverse the migrations. 31 | * 32 | * @return void 33 | */ 34 | public function down() 35 | { 36 | Schema::dropIfExists('likes'); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/database/migrations/2021_02_01_000078_create_follows_table.php: -------------------------------------------------------------------------------- 1 | bigIncrements('id'); 18 | 19 | $table->string('followable_id'); 20 | $table->string('followable_type'); 21 | 22 | $table->string('follower_id'); 23 | $table->string('follower_type'); 24 | 25 | $table->timestamps(); 26 | }); 27 | } 28 | 29 | /** 30 | * Reverse the migrations. 31 | * 32 | * @return void 33 | */ 34 | public function down() 35 | { 36 | Schema::dropIfExists('follows'); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/database/migrations/2021_02_01_000079_create_favourites_table.php: -------------------------------------------------------------------------------- 1 | bigIncrements('id'); 18 | 19 | $table->string('favourable_id'); 20 | $table->string('favourable_type'); 21 | 22 | $table->string('favorer_id'); 23 | $table->string('favorer_type'); 24 | 25 | $table->timestamps(); 26 | }); 27 | } 28 | 29 | /** 30 | * Reverse the migrations. 31 | * 32 | * @return void 33 | */ 34 | public function down() 35 | { 36 | Schema::dropIfExists('favourites'); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/database/migrations/2021_02_01_000080_create_bookmarks_table.php: -------------------------------------------------------------------------------- 1 | bigIncrements('id'); 18 | 19 | $table->string('bookmarkable_id'); 20 | $table->string('bookmarkable_type'); 21 | 22 | $table->string('bookmarker_id'); 23 | $table->string('bookmarker_type'); 24 | 25 | $table->json('data')->nullable(); 26 | $table->timestamps(); 27 | }); 28 | } 29 | 30 | /** 31 | * Reverse the migrations. 32 | * 33 | * @return void 34 | */ 35 | public function down() 36 | { 37 | Schema::dropIfExists('bookmarks'); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/database/migrations/2021_02_01_000081_create_votes_table.php: -------------------------------------------------------------------------------- 1 | bigIncrements('id'); 18 | 19 | $table->string('votable_id'); 20 | $table->string('votable_type'); 21 | 22 | $table->string('voter_id'); 23 | $table->string('voter_type'); 24 | 25 | $table->boolean('vote')->default(true); 26 | $table->timestamps(); 27 | }); 28 | } 29 | 30 | /** 31 | * Reverse the migrations. 32 | * 33 | * @return void 34 | */ 35 | public function down() 36 | { 37 | Schema::dropIfExists('votes'); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/database/migrations/2021_02_01_000082_create_reviews_table.php: -------------------------------------------------------------------------------- 1 | bigIncrements('id'); 18 | 19 | $table->string('reviewable_id'); 20 | $table->string('reviewable_type'); 21 | 22 | $table->string('reviewer_id'); 23 | $table->string('reviewer_type'); 24 | 25 | $table->decimal('rate', 5, 2)->default(0); 26 | $table->string('comment')->nullable(); 27 | 28 | $table->boolean('published')->default(false); 29 | $table->timestamps(); 30 | }); 31 | } 32 | 33 | /** 34 | * Reverse the migrations. 35 | * 36 | * @return void 37 | */ 38 | public function down() 39 | { 40 | Schema::dropIfExists('reviews'); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/database/migrations/2021_02_01_000083_create_subscribes_table.php: -------------------------------------------------------------------------------- 1 | bigIncrements('id'); 18 | 19 | $table->string('subscribable_id'); 20 | $table->string('subscribable_type'); 21 | 22 | $table->string('subscriber_id'); 23 | $table->string('subscriber_type'); 24 | 25 | $table->timestamps(); 26 | }); 27 | } 28 | 29 | /** 30 | * Reverse the migrations. 31 | * 32 | * @return void 33 | */ 34 | public function down() 35 | { 36 | Schema::dropIfExists('subscribes'); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/database/migrations/2021_02_01_000084_create_comments_table.php: -------------------------------------------------------------------------------- 1 | bigIncrements('id'); 18 | 19 | $table->string('commentable_id'); 20 | $table->string('commentable_type'); 21 | 22 | $table->string('commenter_id'); 23 | $table->string('commenter_type'); 24 | 25 | $table->mediumText('comment'); 26 | 27 | $table->timestamps(); 28 | $table->softDeletes(); 29 | }); 30 | } 31 | 32 | /** 33 | * Reverse the migrations. 34 | * 35 | * @return void 36 | */ 37 | public function down() 38 | { 39 | Schema::dropIfExists('comments'); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 |