Skip to content

Commit 2709cf3

Browse files
author
Abdullah Ghanem
authored
Merge pull request #15 from TartanLeGrand/master
Fixed model functions
2 parents e5d4fa2 + ff8782d commit 2709cf3

File tree

3 files changed

+124
-1
lines changed

3 files changed

+124
-1
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ $rating = $post->rating([
7070
dd($rating);
7171
```
7272

73+
### Create or update a unique rating
74+
```php
75+
$user = User::first();
76+
$post = Post::first();
77+
78+
$rating = $post->ratingUnique([
79+
'rating' => 5
80+
], $user);
81+
82+
dd($rating);
83+
```
84+
7385
### Update a rating
7486
```php
7587
$rating = $post->updateRating(1, [
@@ -105,3 +117,17 @@ $post->ratingPercent(10)); // Ten star rating system
105117
// Note: The value passed in is treated as the maximum allowed value.
106118
// This defaults to 5 so it can be called without passing a value as well.
107119
````
120+
121+
### Count positive rating:
122+
````php
123+
$post->countPositive
124+
125+
// $post->countPositive() also works for this.
126+
````
127+
128+
### Count negative rating:
129+
````php
130+
$post->countNegative
131+
132+
// $post->countNegative() also works for this.
133+
````

src/Models/Rating.php

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,60 @@ public function author()
3939
*
4040
* @return static
4141
*/
42-
}
42+
public function createRating(Model $ratingable, $data, Model $author)
43+
{
44+
$rating = new static();
45+
$rating->fill(array_merge($data, [
46+
'author_id' => $author->id,
47+
'author_type' => get_class($author),
48+
]));
49+
50+
$ratingable->ratings()->save($rating);
51+
52+
return $rating;
53+
}
54+
55+
/**
56+
* @param Model $ratingable
57+
* @param $data
58+
* @param Model $author
59+
*
60+
* @return static
61+
*/
62+
public function createUniqueRating(Model $ratingable, $data, Model $author)
63+
{
64+
$rating = [
65+
'author_id' => $author->id,
66+
'author_type' => get_class($author),
67+
"ratingable_id" => $ratingable->id,
68+
"ratingable_type" => get_class($ratingable),
69+
];
70+
71+
Rating::updateOrCreate($rating, $data);
72+
return $rating;
73+
}
74+
75+
/**
76+
* @param $id
77+
* @param $data
78+
*
79+
* @return mixed
80+
*/
81+
public function updateRating($id, $data)
82+
{
83+
$rating = static::find($id);
84+
$rating->update($data);
85+
86+
return $rating;
87+
}
88+
89+
/**
90+
* @param $id
91+
*
92+
* @return mixed
93+
*/
94+
public function deleteRating($id)
95+
{
96+
return static::find($id)->delete();
97+
}
98+
}

src/Traits/Ratingable.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ public function ratingPercent($max = 5)
4545
$total = $this->sumRating();
4646
return ($quantity * $max) > 0 ? $total / (($quantity * $max) / 100) : 0;
4747
}
48+
49+
/**
50+
*
51+
* @return mix
52+
*/
53+
public function countPositive()
54+
{
55+
return $this->ratings()->where('rating', '>', '0')->count();
56+
}
57+
58+
/**
59+
*
60+
* @return mix
61+
*/
62+
public function countNegative()
63+
{
64+
$quantity = $this->ratings()->where('rating', '<', '0')->count();
65+
return ("-$quantity");
66+
}
4867

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

80+
/**
81+
* @param $data
82+
* @param Model $author
83+
* @param Model|null $parent
84+
*
85+
* @return static
86+
*/
87+
public function ratingUnique($data, Model $author, Model $parent = null)
88+
{
89+
return (new Rating())->createUniqueRating($this, $data, $author);
90+
}
91+
6192
/**
6293
* @param $id
6394
* @param $data
@@ -94,4 +125,14 @@ public function getSumRatingAttribute()
94125
{
95126
return $this->sumRating();
96127
}
128+
129+
public function getCountPositiveAttribute()
130+
{
131+
return $this->countPositive();
132+
}
133+
134+
public function getCountNegativeAttribute()
135+
{
136+
return $this->countNegative();
137+
}
97138
}

0 commit comments

Comments
 (0)