PHP code example of codebyray / laravel-review-rateable

1. Go to this page and download the library: Download codebyray/laravel-review-rateable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codebyray / laravel-review-rateable example snippets


php artisan vendor:publish --provider="Codebyray\ReviewRateable\ReviewRateableServiceProvider" --tag=config

php artisan vendor:publish --provider="Codebyray\ReviewRateable\ReviewRateableServiceProvider" --tag=migrations

php artisan migrate



return [
    'min_rating_value' => 1,
    'max_rating_value' => 10,
    'review_approved'  => false, // Reviews will be unapproved by default

    'departments' => [
        'default' => [
            'ratings' => [
                'overall'          => 'Overall Rating',
                'customer_service' => 'Customer Service Rating',
                'quality'          => 'Quality Rating',
                'price'            => 'Price Rating',
                'recommendation'   => 'Would Recommend?',
            ],
        ],
        'sales' => [
            'ratings' => [
                'overall'        => 'Overall Rating',
                'communication'  => 'Communication Rating',
                'follow_up'      => 'Follow-Up Rating',
                'recommendation' => 'Would Recommend?',
            ],
        ],
        'support' => [
            'ratings' => [
                'overall'        => 'Overall Rating',
                'speed'          => 'Response Speed',
                'knowledge'      => 'Knowledge Rating',
                'recommendation' => 'Would Recommend?',
            ],
        ],
    ],
];



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Codebyray\ReviewRateable\Traits\ReviewRateable;

class Product extends Model
{
    use ReviewRateable;
}

$product = Product::find($productId);

$product->addReview([
    'review'     => 'Great product! The quality is superb and customer service was excellent.',
    'department' => 'sales',   // Optional, defaults to 'default'
    'recommend'  => true,      // Whether the user would recommend the product being reviewed
    'approved'   => true,      // Optionally override default (false) approval by providing 'approved'
    'ratings'    => [
        'overall'        => 5,
        'communication'  => 5,
        'follow_up'      => 5,
        'price'          => 5,
    ],
], auth()->id());

// Retrieve the product you want to update the review for.
$product = Product::findOrFail($productId);

// Prepare the updated data.
$data = [
    'review'     => 'Updated review text',    // New review text.
    'department' => 'default',     // Optionally, change the department.
    'recommend'  => false,         // Update recommendation flag.
    'approved'   => true,          // Update approval status if needed.
    'ratings'    => [
        'overall'        => 4,
        'communication'  => 3,
        'follow_up'      => 4,
        'price'          => 2,
    ],
];

// Call the updateReview method on the product.
$product->updateReview($reviewId, $data);

// Retrieve the product you want to mark as approved
$product = Product::findOrFail($productId);

// Approve th review
$product->approveReview($reviewId);

// Retrieve the product with the review you want to delete
$product = Product::findOrFail(1);

// Delete the review
$product->deleteReview($reviewId);

// Approved reviews with ratings
$product = Product::findOrFail($productId);

// Get approved reviews (with related ratings)
// Default: approved = true, withRatings = true
$product->getReviews();

// Get not approved reviews (with related ratings)
$product->getReviews(false);
$product->getReviews(approved: false);

// Get approved reviews (without related ratings)
$product->getReviews(true, false);
$product->getReviews(withRatings: false);

// Retrieve a Product instance (assuming Product uses the ReviewRatable trait)
$product = Product::findOrFail($productId);

// Get the average rating for a specific key ('overall') using approved reviews (default).
$overallAverage = $product->averageRating('overall');
echo "Overall Average (approved): {$overallAverage}\n";

// Get the average rating for a specific key using non-approved reviews.
$nonApprovedOverall = $product->averageRating('overall', false);
echo "Overall Average (pending): {$nonApprovedOverall}\n";

// Get all average ratings for all rating keys from approved reviews.
$allAverages = $product->averageRatings();

// Returns something like
[
    "overall"       => 3.5,
    "communication" => 2.75,
    "follow_up"     => 3.5,
    "price"         => 4.25
]

// Get the overall average rating across all ratings (approved reviews by default).
$overallRating = $product->overallAverageRating();

// Returns float
3.5

// Retrieve a Product instance (assuming Product uses the ReviewRatable trait)
$product = Product::find($productId);

// Get all approved reviews with their ratings eager loaded.
$reviewsWithRatings = $product->getReviews(true, true);

// Get all approved reviews without eager loading ratings.
$reviewsWithoutRatings = $product->getReviews(true, false);

// Retrieve a Product instance (assuming Product uses the ReviewRatable trait)
$product = Product::find($productId);

// Returns the total number of reviews.
$totalReviews = $product->totalReviews();



namespace App\Http\Controllers;

use App\Models\Product;
use CodeByRay\LaravelReviewRatable\Contracts\ReviewRatableContract;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ProductReviewController extends Controller
{
    protected ReviewRatableContract $reviewService;

    public function __construct(ReviewRatableContract $reviewService)
    {
        $this->reviewService = $reviewService;
    }

    public function store(Request $request): JsonResponse
    {
        $product = Product::find($request->input('product_id'));
        $this->reviewService->setModel($product);

        $data = [
            'review'     => $request->input('review'),
            'department' => $request->input('department'),
            'recommend'  => $request->boolean('recommend'),
            // 'approved' is optional and will default to config value.
            'ratings'    => [
                "overall"       => $request->input('overall'),
                "communication" => $request->input('communication'),
                "follow_up"     => $request->input('follow_up'),
                "price"         => $request->input('price')
            ]),
        ];

        $review = $this->reviewService->addReview($data, auth()->id());

        return response()->json(['message' => 'Review added!', 'review' => $review]);
    }

    public function show(Product $product): JsonResponse
    {
        $this->reviewService->setModel($product);

        $reviews = $this->reviewService->getReviews(); // Get approved reviews with ratings
        $total   = $this->reviewService->totalReviews();

        return response()->json(compact('reviews', 'total'));
    }
}