PHP code example of digikraaft / laravel-review-rating

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

    

digikraaft / laravel-review-rating example snippets


//create a review
$author = User::find(1);
$review = "Awesome package! I highly recommend it!!";

$model->review($review, $author);

//write a review and e last review
$model->latestReview(); //returns an instance of \Digikraaft\ReviewRating\Review

//get the review content of the last review
$model->latestReview()->review; //returns 'Awesome package! I highly recommend it!!'

//get the rating of the last review
$model->latestReview()->rating; //return 5

//get the title of the last review
$model->latestReview()->title; //returns 'Lovely packages'

return [
    /*
      * The class name of the review model that holds all reviews.
      *
      * The model must be or extend `Digikraaft\ReviewRating\Review`.
      */
    'review_model' => Digikraaft\ReviewRating\Models\Review::class,

    /*
     * The name of the column which holds the ID of the model related to the reviews.
     *
     * Only change this value if you have set a different name in the migration for the reviews table.
     */
    'model_primary_key_attribute' => 'model_id',

];

use Digikraaft\ReviewRating\Traits\HasReviewRating;
use Illuminate\Database\Eloquent\Model;

class EloquentModel extends Model
{
    use HasReviewRating;
}

$author = User::find(1);
$review = "Awesome package! I highly recommend it!!";

$model->review($review, $author);

$author = User::find(1);
$review = "Awesome package! I highly recommend it!!";

$model->review($review, $author, 5);

$author = User::find(1);
$review = "Awesome package! I highly recommend it!!";

$model->review($review, $author, 5, "Lovely packages");

    if ($model->hasReviewed(auth()->user())) {
        // user has reviewed the model
    }

$model->latestReview(); //returns the latest instance of Digikraaft\ReviewRating\Review

$model->latestReview()->review;

$model->latestReview()->rating;

$model->latestReview()->title;

$model->reviews;

$reviews = $model->reviews;

foreach($reviews as $review){
    echo $review->review . "<br>";
}

$allReviews = EloquentModel::allReviews();

$model->numberOfReviews();

//get the number of reviews a model has received over the last month
$from = now()->subMonth();
$to = now();

$model->numberOfReviews($from, $to);

$model->numberOfRatings();

//get the number of reviews a model has received over the last month
$from = now()->subMonth();
$to = now();

$model->numberOfRatings($from, $to);

$model->averageRating();

//round up to 2 decimal places
$model->averageRating(2);

//get the average rating a model has received over the last month, rounded to 2 decimal places:
$from = now()->subMonth();
$to = now();

$model->averageRating(2, $from, $to);

$allReviewsWithRating = EloquentModel::withRatings();

$model->hasReview();

$model->hasRating();

namespace Digikraaft\ReviewRating\Events;

use Digikraaft\ReviewRating\Models\Review;
use Illuminate\Database\Eloquent\Model;

class ReviewCreatedEvent
{
    /** @var \Digikraaft\ReviewRating\Models\Review */
    public Review $review;

    public function __construct(Review $review)
    {
        $this->review = $review;
    }
}
bash
php artisan vendor:publish --provider="Digikraaft\ReviewRating\ReviewRatingServiceProvider" --tag="migrations"
bash
php artisan migrate
bash
php artisan vendor:publish --provider="Digikraaft\ReviewRating\ReviewRatingServiceProvider" --tag="config"