Skip to content

Commit

Permalink
Merge pull request #15 from TartanLeGrand/master
Browse files Browse the repository at this point in the history
Fixed model functions
  • Loading branch information
Abdullah Ghanem authored Apr 25, 2017
2 parents e5d4fa2 + ff8782d commit 2709cf3
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1 deletion.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ $rating = $post->rating([
dd($rating);
```

### Create or update a unique rating
```php
$user = User::first();
$post = Post::first();

$rating = $post->ratingUnique([
'rating' => 5
], $user);

dd($rating);
```

### Update a rating
```php
$rating = $post->updateRating(1, [
Expand Down Expand Up @@ -105,3 +117,17 @@ $post->ratingPercent(10)); // Ten star rating system
// Note: The value passed in is treated as the maximum allowed value.
// This defaults to 5 so it can be called without passing a value as well.
````

### Count positive rating:
````php
$post->countPositive

// $post->countPositive() also works for this.
````

### Count negative rating:
````php
$post->countNegative

// $post->countNegative() also works for this.
````
58 changes: 57 additions & 1 deletion src/Models/Rating.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,60 @@ public function author()
*
* @return static
*/
}
public function createRating(Model $ratingable, $data, Model $author)
{
$rating = new static();
$rating->fill(array_merge($data, [
'author_id' => $author->id,
'author_type' => get_class($author),
]));

$ratingable->ratings()->save($rating);

return $rating;
}

/**
* @param Model $ratingable
* @param $data
* @param Model $author
*
* @return static
*/
public function createUniqueRating(Model $ratingable, $data, Model $author)
{
$rating = [
'author_id' => $author->id,
'author_type' => get_class($author),
"ratingable_id" => $ratingable->id,
"ratingable_type" => get_class($ratingable),
];

Rating::updateOrCreate($rating, $data);
return $rating;
}

/**
* @param $id
* @param $data
*
* @return mixed
*/
public function updateRating($id, $data)
{
$rating = static::find($id);
$rating->update($data);

return $rating;
}

/**
* @param $id
*
* @return mixed
*/
public function deleteRating($id)
{
return static::find($id)->delete();
}
}
41 changes: 41 additions & 0 deletions src/Traits/Ratingable.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ public function ratingPercent($max = 5)
$total = $this->sumRating();
return ($quantity * $max) > 0 ? $total / (($quantity * $max) / 100) : 0;
}

/**
*
* @return mix
*/
public function countPositive()
{
return $this->ratings()->where('rating', '>', '0')->count();
}

/**
*
* @return mix
*/
public function countNegative()
{
$quantity = $this->ratings()->where('rating', '<', '0')->count();
return ("-$quantity");
}

/**
* @param $data
Expand All @@ -58,6 +77,18 @@ public function rating($data, Model $author, Model $parent = null)
return (new Rating())->createRating($this, $data, $author);
}

/**
* @param $data
* @param Model $author
* @param Model|null $parent
*
* @return static
*/
public function ratingUnique($data, Model $author, Model $parent = null)
{
return (new Rating())->createUniqueRating($this, $data, $author);
}

/**
* @param $id
* @param $data
Expand Down Expand Up @@ -94,4 +125,14 @@ public function getSumRatingAttribute()
{
return $this->sumRating();
}

public function getCountPositiveAttribute()
{
return $this->countPositive();
}

public function getCountNegativeAttribute()
{
return $this->countNegative();
}
}

0 comments on commit 2709cf3

Please sign in to comment.