1. Go to this page and download the library: Download secrethash/r8 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/ */
secrethash / r8 example snippets
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;
}
public function show($id)
{
$post = Post::find($id);
return view('post.show')->with('post', $post);
}
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);
}
}
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();
}
}
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]);
}
}
@foreach($post->reviews as $review)
...
Total Ratings: {{ $review->ratings->count() }}
...
@endforeach
@foreach($post->reviews as $review)
...
Average Ratings: {{ $review->ratings->average('value') }}
...
@endforeach