| Total Complexity | 5 |
| Total Lines | 45 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
| 1 | <?php |
||
| 18 | trait UserHasInteraction |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Get all likes of the user. This method is used for eager loading. |
||
| 22 | * |
||
| 23 | * @return HasMany |
||
| 24 | */ |
||
| 25 | public function likes(): HasMany |
||
| 26 | { |
||
| 27 | $interactionModel = (string) (config('like.interaction_model') ?? Like::class); |
||
| 28 | $userForeignKey = (string) (config('like.users.foreign_key') ?? 'user_id'); |
||
| 29 | |||
| 30 | return $this->hasMany($interactionModel, $userForeignKey); |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Check if the user has liked the given model. |
||
| 35 | * |
||
| 36 | * @param string $interactionType |
||
| 37 | * |
||
| 38 | * @return static |
||
| 39 | */ |
||
| 40 | public function forgetInteractionsOfType(string $interactionType): static |
||
| 41 | { |
||
| 42 | $this->likes()->where('type', $interactionType)->delete(); |
||
| 43 | |||
| 44 | return $this; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Check if the user has liked the given model. |
||
| 49 | * |
||
| 50 | * @param null|string $interactionType |
||
| 51 | * |
||
| 52 | * @return static |
||
| 53 | */ |
||
| 54 | public function forgetInteractions(?string $interactionType = null): static |
||
| 63 | } |
||
| 64 | } |
||
| 65 |