Laravel 11 书评网6--显示主页图书+详情页评分星级及评论计数

86 阅读1分钟

显示前端的平均图书评分星级和评论计数

App/Http/Controllers/HomeController.php

// 首页
public function index(Request $request)
{
    $books = Book::withCount('reviews')->withSum('reviews', 'rating')->orderBy('created_at', 'DESC');

    // 搜索标题
    if (!empty($request->keyword)) {
        $books->where('title', 'like', '%' . $request->keyword . '%');
    };

    $books = $books->where('status', 1)->paginate(8);

    return view('home', compact('books'));
}

// 显示首页书籍详情页
public function detail($id)
{
    // 查询当前书籍详情页内容和评论
    $book = Book::with(['reviews.user', 'reviews' => function ($query) {
        $query->where('status', 1);
    }])->withCount('reviews')->withSum('reviews', 'rating')->findOrFail($id);

    // 如果书籍是关闭状态就404
    if ($book->status == 0) {
        abort(404);
    }

    // 生成读者还喜欢的随机书籍  take(3)读取最近三条信息 inRandomOrder()随机排序
    $relatedBooks = Book::where('status', 1)
        ->withCount('reviews')
        ->withSum('reviews', 'rating')
        ->take(3)->where('id', '!=', $id)->inRandomOrder()->get();

    return view('book-detail', compact('book', 'relatedBooks'));
}
前端首页页面

resources/views/home.blade.php

@php
    if ($book->reviews_count > 0){
        $avgRating = $book->reviews_sum_rating/$book->reviews_count;
    } else {
        $avgRating = 0;
    }

    $avgRatingPer =($avgRating*100)/5;
@endphp

<div class="star-rating d-inline-flex ml-2" title="">
    <span
        class="rating-text theme-font theme-yellow">{{ number_format($avgRating,1) }}</span>
    <div class="star-rating d-inline-flex mx-2" title="">
        <div class="back-stars ">
            <i class="fa fa-star " aria-hidden="true"></i>
            <i class="fa fa-star" aria-hidden="true"></i>
            <i class="fa fa-star" aria-hidden="true"></i>
            <i class="fa fa-star" aria-hidden="true"></i>
            <i class="fa fa-star" aria-hidden="true"></i>

            <div class="front-stars" style="width: {{$avgRatingPer}}%">
                <i class="fa fa-star" aria-hidden="true"></i>
                <i class="fa fa-star" aria-hidden="true"></i>
                <i class="fa fa-star" aria-hidden="true"></i>
                <i class="fa fa-star" aria-hidden="true"></i>
                <i class="fa fa-star" aria-hidden="true"></i>
            </div>
        </div>
    </div>
    <span class="theme-font text-muted">(
        {{ ($book->reviews_count > 1)? $book->reviews_count.'评论' : $book->reviews_count.'评论'}}
        )</span>
</div>
前端书籍详情页

resources/views/book-detail.blade.php

@php
    if ($book->reviews_count > 0){
        $avgRating = $book->reviews_sum_rating/$book->reviews_count;
    } else {
        $avgRating = 0;
    }

    $avgRatingPer =($avgRating*100)/5;
@endphp

<div class="col-md-8">
    {{-- 表单验证文件 --}}
    @include('layouts.message')

    <h3 class="h2 mb-3">{{ $book->title }}</h3>
    <div class="h4 text-muted">{{ $book->author }}</div>
    <div class="star-rating d-inline-flex ml-2" title="">
        <span class="rating-text theme-font theme-yellow">{{ number_format($avgRating,1) }}</span>
        <div class="star-rating d-inline-flex mx-2" title="">
            <div class="back-stars ">
                <i class="fa fa-star " aria-hidden="true"></i>
                <i class="fa fa-star" aria-hidden="true"></i>
                <i class="fa fa-star" aria-hidden="true"></i>
                <i class="fa fa-star" aria-hidden="true"></i>
                <i class="fa fa-star" aria-hidden="true"></i>

                <div class="front-stars" style="width: {{$avgRatingPer}}%">
                    <i class="fa fa-star" aria-hidden="true"></i>
                    <i class="fa fa-star" aria-hidden="true"></i>
                    <i class="fa fa-star" aria-hidden="true"></i>
                    <i class="fa fa-star" aria-hidden="true"></i>
                    <i class="fa fa-star" aria-hidden="true"></i>
                </div>
            </div>
        </div>
        <span class="theme-font text-muted">(
            {{ ($book->reviews_count > 1) ? $book->reviews_count.'评论' : $book->reviews_count.'评论'}}
            )</span>
    </div>
@php
    if ($relatedBook->reviews_count > 0){
        $avgRating = $relatedBook->reviews_sum_rating/$relatedBook->reviews_count;
    } else {
        $avgRating = 0;
    }

    $avgRatingPer =($avgRating*100)/5;
@endphp

<div class="card-body">
    <h3 class="h4 heading"><a
            href="{{ route("book.detail",$relatedBook->id) }}">{{ $relatedBook->title }}</a>
    </h3>
    <p>{{ $relatedBook->author }}</p>
    <div class="star-rating d-inline-flex ml-2" title="">
        <span
            class="rating-text theme-font theme-yellow">{{ number_format($avgRating,1) }}</span>
        <div class="star-rating d-inline-flex mx-2" title="">
            <div class="back-stars ">
                <i class="fa fa-star " aria-hidden="true"></i>
                <i class="fa fa-star" aria-hidden="true"></i>
                <i class="fa fa-star" aria-hidden="true"></i>
                <i class="fa fa-star" aria-hidden="true"></i>
                <i class="fa fa-star" aria-hidden="true"></i>

                <div class="front-stars" style="width: {{$avgRatingPer}}%">
                    <i class="fa fa-star" aria-hidden="true"></i>
                    <i class="fa fa-star" aria-hidden="true"></i>
                    <i class="fa fa-star" aria-hidden="true"></i>
                    <i class="fa fa-star" aria-hidden="true"></i>
                    <i class="fa fa-star" aria-hidden="true"></i>
                </div>
            </div>
        </div>
        <span class="theme-font text-muted">(
            {{ ($book->reviews_count > 1) ? $book->reviews_count.'评论' : $book->reviews_count.'评论'}}
            )
        </span>

显示后台个人主页评论数

定义模型 App/Models/User.php

// 用户下的评论
public function reviews(): HasMany
{
    return $this->hasMany(Review::class);
}
前端模版

resources/views/layouts/sidebar.blade.php

<div class="h5 text-center">
    <strong>{{ Auth::user()->name }}</strong>
    <p class="h6 mt-2 text-muted">
        {{ (Auth::user()->reviews->count() > 1)? Auth::user()->reviews->count().'评论' : Auth::user()->reviews->count().'评论'}}
    </p>
</div>

显示后端书籍的评分及评论数

App/Http/Controllers/BookControllerController.php

// 书籍首页
public function index(Request $request)
{
    $books = $books->withCount('reviews')->withSum('reviews', 'rating')->paginate(3);
}
前端模版

resources/views/books/list.blade.php

@php
    if ($book->reviews_count > 0){
        $avgRating = $book->reviews_sum_rating/$book->reviews_count;
    } else {
        $avgRating = 0;
    }

    $avgRatingPer =($avgRating*100)/5;
@endphp

<tr>
    <td>{{ $book->title }}</td>
    <td>{{ $book->author }}</td>
    <td>{{ number_format($avgRating,1) }}
        (
        {{ ($book->reviews_count > 1) ? $book->reviews_count.'评论' : $book->reviews_count.'评论'}}
        )
    </td>