Skip to content

Commit

Permalink
chore: update tutorials
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhongit committed Sep 26, 2024
1 parent d5ef492 commit e938f86
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
10 changes: 8 additions & 2 deletions config/like.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
];
48 changes: 25 additions & 23 deletions src/Models/Like.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
*/
Expand Down Expand Up @@ -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<Model, self>
*/
public function model(): BelongsTo
{
return $this->morphTo();
}

/**
* Check if the record is liked.
*
Expand Down Expand Up @@ -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<Model, self>
*/
public function model(): BelongsTo
{
return $this->morphTo();
}

/**
* Toggle the like interaction.
*
Expand Down

0 comments on commit e938f86

Please sign in to comment.