Skip to content

Commit 7625d21

Browse files
committed
Updated docs.
1 parent cdc271d commit 7625d21

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,5 +238,62 @@ $product = Product::find($productId);
238238
$totalReviews = $product->totalReviews();
239239
````
240240

241+
## Example Usage in a Controller
242+
Every method available using the ReviewRateable Trait can also be called via the service
243+
244+
```php
245+
<?php
246+
247+
namespace App\Http\Controllers;
248+
249+
use App\Models\Product;
250+
use CodeByRay\LaravelReviewRatable\Contracts\ReviewRatableContract;
251+
use Illuminate\Http\JsonResponse;
252+
use Illuminate\Http\Request;
253+
254+
class ProductReviewController extends Controller
255+
{
256+
protected ReviewRatableContract $reviewService;
257+
258+
public function __construct(ReviewRatableContract $reviewService)
259+
{
260+
$this->reviewService = $reviewService;
261+
}
262+
263+
public function store(Request $request): JsonResponse
264+
{
265+
$product = Product::find($request->input('product_id'));
266+
$this->reviewService->setModel($product);
267+
268+
$data = [
269+
'review' => $request->input('review'),
270+
'department' => $request->input('department'),
271+
'recommend' => $request->boolean('recommend'),
272+
// 'approved' is optional and will default to config value.
273+
'ratings' => [
274+
"overall" => $request->input('overall'),
275+
"communication" => $request->input('communication'),
276+
"follow_up" => $request->input('follow_up'),
277+
"price" => $request->input('price')
278+
]),
279+
];
280+
281+
$review = $this->reviewService->addReview($data, auth()->id());
282+
283+
return response()->json(['message' => 'Review added!', 'review' => $review]);
284+
}
285+
286+
public function show(Product $product): JsonResponse
287+
{
288+
$this->reviewService->setModel($product);
289+
290+
$reviews = $this->reviewService->getReviews(); // Get approved reviews with ratings
291+
$total = $this->reviewService->totalReviews();
292+
293+
return response()->json(compact('reviews', 'total'));
294+
}
295+
}
296+
```
297+
241298
### Notes
242299

0 commit comments

Comments
 (0)