Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
milwad-dev committed Nov 26, 2024
1 parent da3fe5b commit 1d4c835
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/Traits/InteractWithScore.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
namespace Binafy\LaravelScore\Traits;

use Binafy\LaravelScore\Models\Score;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;

trait InteractWithScore
{
/**
* Relation one-to-many, Score model.
*/
public function scores(): \Illuminate\Database\Eloquent\Relations\HasMany
public function scorings(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(
config('laravel-score.model'),
config('laravel-score.user.foreign_key'),
config('laravel-score.model', \Binafy\LaravelScore\Models\Score::class),
config('laravel-score.user_foreign_key', 'user_id'),
$this->getKeyName()
);
}
Expand All @@ -27,7 +26,7 @@ public function addScore(Model $scoreable, int $score = 1, int|null $userId = nu
{
return Score::query()->create([
'scoreable_id' => $scoreable->getKey(),
'scoreable_type' => $scoreable::class,
'scoreable_type' => $scoreable->getMorphClass(),
'user_id' => $userId ?? auth()->id(),
'score' => $score,
]);
Expand All @@ -40,7 +39,7 @@ public function addNegativeScore(Model $scoreable, int|null $userId = null)
{
return Score::query()->create([
'scoreable_id' => $scoreable->getKey(),
'scoreable_type' => $scoreable::class,
'scoreable_type' => $scoreable->getMorphClass(),
'user_id' => $userId ?? auth()->id(),
'score' => -1,
]);
Expand Down
11 changes: 11 additions & 0 deletions src/Traits/Scoreable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ public function scores(): \Illuminate\Database\Eloquent\Relations\MorphMany
);
}

/**
*
*/
public function scoreable(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(
config('laravel-score.model', \Binafy\LaravelScore\Models\Score::class),
'scoreable_id',
)->where('scoreable_type', $this->getMorphClass());
}

/**
* Get positive score relation.
*/
Expand Down

0 comments on commit 1d4c835

Please sign in to comment.