| 1 | <?php |
||
| 4 | trait HasRoles |
||
| 5 | { |
||
| 6 | |||
| 7 | /** |
||
| 8 | * A user may have multiple roles. |
||
| 9 | * |
||
| 10 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
| 11 | */ |
||
| 12 | public function roles() |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Assign the given role to the user. |
||
| 19 | * |
||
| 20 | * @param string $role |
||
| 21 | * @return mixed |
||
| 22 | */ |
||
| 23 | public function assignRole($role) |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Determine if the user has the given role. |
||
| 32 | * |
||
| 33 | * @param mixed $role |
||
| 34 | * @return boolean |
||
| 35 | */ |
||
| 36 | public function hasRole($role) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Determine if the user may perform the given permission. |
||
| 47 | * |
||
| 48 | * @param Permission $permission |
||
| 49 | * @return boolean |
||
| 50 | */ |
||
| 51 | public function hasPermission($permission) |
||
| 59 | |||
| 60 | } |
||
| 61 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.