Adding a Star Rating system to your application – Step 7 – Drilling down on the average rating

In order to drill down on the ratings behind the average, we need a simple page which will show a list of ratings for a specific given court. To do this we can use the standard court ratings view and modify it but we will need to adapt things slightly so that we can pass as specific courtid to the view to ensure it only displays ratings for that view. In this way, when we click on the ratings on the courts page for a specific court we can pass that courtid through the controller and only retrieve the ratings for that specific court before we pass them through to the view.

Modify the file /app/controllers/CourtratingController.php and add the following function

     public function showcourtratings($courtid)
{
$courtratings = $this->courtratingRepository->all()->where('courtid',$courtid);
return view('courtratings.index')
->with('courtratings', $courtratings);
}

This function finds only the ratings for the court passed in the $courtid variable and will then pass those ratings to the courtratings.index view.

To make this controller function work, we need a corresponding route. Modify /routes/web.php and add the following route

Route::get('/courtratings/court/{court}','courtratingController@showcourtratings')->name('courtratings.showcourtratings');

Finally, to modify the courts index page to allow us to click through on a star rating to see what's behind it - open the view /resources/views/courts/table.blade.php and modify it so the a the input tag which displays the stars is wrapped in an achor tag which will link to the route to display courtratings just for that specific court.

To ensure that the courtratings index view also shows the star ratings graphically. Modify the view /resources/views/courtratings/table.blade.php and add the following lines

Now the court ratings, when you drill down on them, should appear as follows:

Leave a Reply