PHP code example of multek / laravel-review

1. Go to this page and download the library: Download multek/laravel-review 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/ */

    

multek / laravel-review example snippets


use Multek\Review\Traits\HasReviews;

class Product extends Model
{
    use HasReviews;
}

use Multek\Review\Traits\CanReview;

class User extends Model
{
    use CanReview;
}

// Via the reviewable model
$product->addReview([
    'rating' => 5,
    'title' => 'Amazing!',
    'body' => 'Best product ever.',
], $user);

// Via the author model
$user->review($product, [
    'rating' => 4,
    'body' => 'Pretty good.',
]);

$summary = $product->review_summary;

$summary->average_rating;       // 4.5
$summary->total_count;          // 42
$summary->rating_distribution;  // [1 => 2, 2 => 3, 3 => 5, 4 => 12, 5 => 20]
$summary->percentage(5);        // 47.6

$review->approve();
$review->reject();
$review->addReply('Thank you for your feedback!');

$review->isApproved();
$review->isPending();
$review->isRejected();

// On Review model
Review::approved()->get();
Review::pending()->get();
Review::byAuthor($user)->get();

// On reviewable models
Product::orderByAverageRating('desc')->get();
Product::orderByReviewCount('desc')->get();
Product::hasMinimumRating(4)->get();
Product::hasMinimumReviews(10)->get();
Product::withReviewSummary()->get();

return [
    'min_rating' => 1,
    'max_rating' => 5,
    'allow_decimals' => false,
    'auto_approve' => true,
    'cache' => [
        'enabled' => true,
        'ttl' => 3600,
        'store' => null,
        'prefix' => 'review_',
    ],
    'review_model' => \Multek\Review\Models\Review::class,
];
bash
php artisan vendor:publish --tag=review-migrations
php artisan migrate
bash
php artisan vendor:publish --tag=review-config