PHP Classes

R8 PHP Review System: Add reviews, ratings and recommendations to a site

Recommend this page to a friend!
  Info   View files Documentation   View files View files (18)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2020-06-04 (Yesterday) RSS 2.0 feedNot yet rated by the usersTotal: 23 This week: 23All time: 9,924 This week: 17Up
Version License PHP version Categories
r8 1.0Custom (specified...5PHP 5, Libraries, Content management
Description Author

This package can be used to add reviews, ratings and recommendations to a site.

It provides classes and traits that can display forms for taking user contributed reviews, ratings and recommendations.

The package can also store the values of the evaluation of the content of the users and store it in a database table records associated to the models of the content that is evaluated.

Name: Shashwat Mishra <contact>
Classes: 2 packages by
Country: India India

Details

R8: Reviews, Ratings and Recommendations

Latest Stable Version Total Downloads Dependents Latest Unstable Version License

Laravel has always been missing a package like this, that supports dynamic rating with multiple Rating Types (ex. Like in Amazon or any e-commerce platform, Quality Rating, Customer Service Experience Ratings, etc.) with Integrated Reviews and Recommend Functionality.

The main Ideology behind this package is to make it easily adaptable for everyone's use case.

Reviews & Ratings system for Laravel 7. You can rate any of your models.

  • Custom Rating Types (ex: Product Quality, Delivery Speed, Pricing, etc.) without any limitations.
  • Display Overall and Average Ratings
  • Method Chaining
  • You can set whether the model being rated is recommended.

Installation

First, pull in the package through Composer.

composer require secrethash/r8

NOTE: The dev-master is _UNDER HEAVY DEVELOPMENT_

You will need to publish and run the migrations.

php artisan vendor:publish --provider="Secrethash\R8\R8ServiceProvider" --tag="migrations"

Run the migrations

php artisan migrate

Setup

Setup the model that will be reviewed, rated & recommended.

<?php

namespace App;

use Secrethash\R8\Contracts\R8;
use Secrethash\R8\Traits\R8Trait;
use Illuminate\Database\Eloquent\Model;

class Post extends Model implements R8
{
    
    use R8Trait;

}

Usage

Things will be kept simple. But to understand usage, you must understand the methodology behind it.

Methodology

The Methodology used is: | Utility | Relation | With | |---------|------------|-----------| |Review | HasMany | Ratings | |Ratings | BelongsTo |RatingTypes| |Recommend|is a part of| Reviews |

Fetch

Taking Simple Laravel Examples.

App\Http\Controllers\PostController.php

	public function show($id)
	{
		$post = Post::find($id);

		return view('post.show')->with('post', $post);
	}

Reviews & Recommend

resources/views/post/show.blade.php

@foreach($post->reviews as $review)

	<h2> {{ $review->title }} </h2>
	<p> {{ $review->body }} </p>
	<span> Recommended: {{ $review->recommend }} </span>
	<span> By: {{ $review->author->name }} </span>

@endforeach

Ratings

NOTE: A Review must be created first to Create and link One or Many ratings with it.

resources/views/post/show.blade.php

@foreach($post->reviews as $review)

	<h2> {{ $review->title }} </h2>
	<p> {{ $review->body }} </p>
	<span> Recommended: {{ $review->recommend }} </span>
	<span> By: {{ $review->author->name }} </span>

	@foreach ($review->ratings as $rating)
		{{ $rating->type->name }}
		{{ $rating->value }}
	@endforeach

@endforeach

Create

Reviews

App\Http\Controllers\ReviewController.php

<?php

namespace App\Http\Controllers;

use App\Post;

class ReviewController {

    /
     * Store a newly created resource in storage.
     *
	 * @param $id
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
	public function store($id, Request $request)
	{
		$post = Post::find($id);

		// Create Review
		$review = $post->reviews()->create([
            		'title' => 'One More Bad Sample Review',
            		'body' => 'This is a another new sample review. This one has 4 Type Reviews.',
            		'recommend' => 'No',
		]);

		// Associate Author User ID
        $review->author()->associate(auth()->user()->id);

	}
}

Rating

> Assumption: A Rating Type has already been created with 'slug' => 'customer-service'.

App\Http\Controllers\ReviewController.php

<?php

namespace App\Http\Controllers;

use App\Post;
use Secrethash\R8\Models\RateType;

class ReviewController {

    /
     * Store a newly created resource in storage.
     *
	 * @param $id
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
	public function store($id, Request $request)
	{
		$post = Post::find($id);

		// Getting the ID
		$type = RateType::where('slug', 'customer-service')->first();

		// Create Review
		$review = $post->reviews()->create([
            		'title' => 'One More Bad Sample Review',
            		'body' => 'This is a another new sample review. This one has 4 Type Reviews.',
            		'recommend' => 'Yes', // Enum: accepts 'Yes' or 'No'
		]);

		// Associate Author User ID
		$review->author()->associate(auth()->user()->id);

		// Creating Rating
		$rating = $review->ratings()->create([
			'value' => 5
		]);

		// Associate Rating Type ID
		$rating->type()->associate($type->id);

		// Saving Everything
		$review->save();
		$rating->save();

	}
}

RateTypes

Similarly, Secrethash\R8\Models\RateType can be used to create Rating Types like _Product Quality_, _Customer Service_, _Delivery_, etc.

| Fillable | Description | | -------- | ------------- | | slug | Sluggish Name | | name | Full Name |

Other Operations

To keep things simple, Operations like Counting Reviews and Ratings are done through well defined relations, the Laravel Way.

Counting

Reviews

<?php

namespace App\Http\Controllers;

use App\Post;

class ReviewController {

	public function show($id)
	{
		$post = Post::find($id);

		$reviews = $post->reviews->count();

		$approved = $post->reviews
				 ->where('approved', 1)
				 ->count();

		return view('post.show')->with(['post' => $post, 'reviews' => $reviews, 'approved' => $approved]);
	}
}

Ratings

Assuming the counting will be performed directly in the blade.php file.

resources/views/post/show.blade.php

@foreach($post->reviews as $review)
	...
	Total Ratings: {{ $review->ratings->count() }}
	...
@endforeach

Average

Ratings

Laravel manages collections in a smart way. And as we know that "All multi-result sets returned by Eloquent are instances of the Illuminate\Database\Eloquent\Collection object" <sup>[Laravel Docs][1]</sup> All you need to do is pass in the name of the table (here value) in the average() collection helper method.

resources/views/post/show.blade.php

@foreach($post->reviews as $review)
	...
	Average Ratings: {{ $review->ratings->average('value') }}
	...
@endforeach

Contributions

Contributions are welcomed and appreciated. Feel free to create a Pull Request or an Issue.

Database Structure

As this package is still under development, the database structure might change in future. The structure ER Diagram is provided as XML File and also as an Image File. Database Entity Relation Diagram

License

This project is Licensed under MIT. See the License File for more information.

Note

> This repository has been forked from codebyray/laravel-review-rateable

It was forked initially as a head-start and a lot has been changed since then. The whole concept and methodology has been changed.

Please note that the original code does not matches the code from this repository as a lot has been changed.

[1]: https://laravel.com/docs/7.x/eloquent-collections "Laravel Docs for Eloquent Collections"

  Files folder image Files  
File Role Description
Files folder imagedatabase (1 directory)
Files folder imagesrc (1 file, 3 directories)
Files folder imagetests (1 file)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Image file database_structure.png Data Auxiliary data
Accessible without login Plain text file db.rd.xml Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  database  
File Role Description
Files folder imagemigrations (3 files)

  Files folder image Files  /  database  /  migrations  
File Role Description
  Plain text file create_rate_types_table.php.stub Class Class source
  Plain text file create_ratings_table.php.stub Class Class source
  Plain text file create_reviews_table.php.stub Class Class source

  Files folder image Files  /  src  
File Role Description
Files folder imageContracts (1 file)
Files folder imageModels (3 files)
Files folder imageTraits (1 file)
  Plain text file R8ServiceProvider.php Class Class source

  Files folder image Files  /  src  /  Contracts  
File Role Description
  Plain text file R8.php Class Class source

  Files folder image Files  /  src  /  Models  
File Role Description
  Plain text file RateType.php Class Class source
  Plain text file Rating.php Class Class source
  Plain text file Review.php Class Class source

  Files folder image Files  /  src  /  Traits  
File Role Description
  Plain text file R8Trait.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file TestCase.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:23
This week:23
All time:9,924
This week:17Up

For more information send a message to info at phpclasses dot org.