我正在使用 Laravel:Livewire 星级组件。我想以星星显示总评分

Posted

技术标签:

【中文标题】我正在使用 Laravel:Livewire 星级组件。我想以星星显示总评分【英文标题】:I'm using Laravel:Livewire star rating Components. I want to show total ratings in stars 【发布时间】:2021-11-24 05:00:41 【问题描述】:

评分模块运行良好,评分数也显示,但我想以星数显示总评分但我不知道如何在刀片视图中书写。

Livewire/ProductRating.php

public $rating;
    public $comment;
    public $currentId;
    public $product;
    public $hideForm;

    protected $rules = [
        'rating' => ['required', 'in:1,2,3,4,5'],
        'comment' => 'required',

    ];

    public function render()
    
        $comments = Rating::where('product_id', $this->product->id)->where('status', 1)->with('user')->get();
        return view('livewire.product-ratings', compact('comments'));
    

    public function mount()
    
        if (auth()->user()) 
            $rating = Rating::where('user_id', auth()->user()->id)->where('product_id', $this->product->id)->first();
            if (!empty($rating)) 
                $this->rating  = $rating->rating;
                $this->comment = $rating->comment;
                $this->currentId = $rating->id;
            
        
        return view('livewire.product-ratings');
    

【问题讨论】:

您能否进一步详细说明I want to show total ratings in stars,或者提供一个示例?您是要显示平均评分总数,还是按评分显示评分总数? 我已经统计了评分总数。我想用星号显示这些评级。就像有些人给 4 分,有些人给 5 分一样。所以我想用星号显示所有这些评分。 可能就是所谓的平均 是的,对我来说这听起来像是average 如何在刀片中做到这一点 【参考方案1】:

假设您的 Rating 模型上有一个名为 rating 的属性,您可以执行以下操作以获取具有给定 id 的产品的平均评分:

$average = Rating::where('product_id', $id)->avg('rating');

【讨论】:

以上是关于我正在使用 Laravel:Livewire 星级组件。我想以星星显示总评分的主要内容,如果未能解决你的问题,请参考以下文章

Laravel Livewire:组件还是控制器? [关闭]

Laravel Livewire 绑定模型 Carbon 属性

Laravel Livewire 组件偏移

Laravel(Livewire)中的组件拆分和保留参数

如何从 Laravel Livewire 的属性中删除上传的文件?

Laravel Livewire 在一页中进行多个分页