diff --git a/config/like.php b/config/like.php index 1d3a9cf..623c8ce 100644 --- a/config/like.php +++ b/config/like.php @@ -23,10 +23,16 @@ * The model and foreign key for the user relationship. */ 'users' => [ - /* User model class. */ + /* + * User model class. + * Use this to set the user model class for the user relationship. + */ 'model' => 'App\Models\User', - /* User tables foreign key name. */ + /* + * User tables foreign key name. + * Use this to set the foreign key name for the user relationship. + */ 'foreign_key' => 'user_id', ], ]; diff --git a/src/Models/Like.php b/src/Models/Like.php index e027de7..a75dcbd 100644 --- a/src/Models/Like.php +++ b/src/Models/Like.php @@ -20,6 +20,8 @@ * @property InteractionTypeEnum $type * @property Carbon $created_at * @property Carbon $updated_at + * @property Model $user + * @property Model $model * * @property-read string $interaction_type getInteractionTypeAttribute() */ @@ -47,6 +49,29 @@ class Like extends Model 'type' => InteractionTypeEnum::class, ]; + /** + * Get the user that owns the like. + * + * @return BelongsTo + */ + public function user(): BelongsTo + { + $userModel = (string) (config('like.users.model') ?? config('auth.providers.users.model')); + $userForeignKey = (string) (config('like.users.foreign_key') ?? 'user_id'); + + return $this->belongsTo($userModel, $userForeignKey); + } + + /** + * Get the model that the like belongs to. + * + * @return BelongsTo + */ + public function model(): BelongsTo + { + return $this->morphTo(); + } + /** * Check if the record is liked. * @@ -110,29 +135,6 @@ public function getInteractionTypeAttribute(): string return $this->type->value; } - /** - * Get the user that owns the like. - * - * @return BelongsTo - */ - public function user(): BelongsTo - { - $userModel = (string) (config('like.users.model') ?? config('auth.providers.users.model')); - $userForeignKey = (string) (config('like.users.foreign_key') ?? 'user_id'); - - return $this->belongsTo($userModel, $userForeignKey); - } - - /** - * Get the model that the like belongs to. - * - * @return BelongsTo - */ - public function model(): BelongsTo - { - return $this->morphTo(); - } - /** * Toggle the like interaction. *