允许在 Laravel Nova 上使用类似五星级系统对多个模型进行评分

v0.13.1 2023-10-13 12:20 UTC

README

关于

此包向您的 Laravel Nova 模型引入了 可评分特质

此包向您的应用程序模型引入了 可评分特质

此外,它还引入了一个交互式 字段 到您的 Nova 后端,使您可以立即对任何您希望的属性进行评分。

使用此包可以进行多类别评分。

例如,您可能想对以下内容进行评分:

  • 性能
  • 价格
  • 质量

的产品。

但是,您也可以通过使用虚拟属性 averageRating 来生成返回所有三个类别平均评分的字段。

配置设置

运行 php artisan vendor:publish --tag=config 来设置默认设置。您可以在 config/rating.php 中找到它们。

您还可以通过调用 starSizepadding 方法为每个字段条目设置特定的尺寸和填充。

致谢

感谢您做基础工作

https://github.com/willvincent/laravel-rateable https://github.com/craigh411/vue-star-rating

特别致谢

感谢您的启发

https://novapackages.com/packages/nikaia/nova-rating-field

如果您需要有关可能选项的更多文档,这可能是一个好的文档资源。

安装

  1. composer require kosmoskosmos/rating
  2. php artisan vendor:publish
  3. php artisan migrate

使用

public function fields(Request $request)
{
    return [
        // ...
        // Define categories for to be rated.
        Rating::make('Pizza Baking Skills', 'pizza_rating')->hideFromIndex(),
        Rating::make('Sushi Rolling Skills',  'sushi_rating')->hideFromIndex(),
        Rating::make('Bread Baking Skills', 'bread_rating')->hideFromIndex(),
        // Show average rating from all three categories above.  
        Rating::make('Overall Skills', 'average_rating')->onlyOnIndex(), 
        // Show average rating from only one category above
        Rating::make('Overall Bread Baking Skills', 'bread_average_rating')->onlyOnIndex(),  
    ];
}

扩展您的模型如下

class MyModel extends Model {
    // use the Rateable trait 
    use \KosmosKosmos\Rating\Rateable;
    
    // use the getAttribute() function to inject the Trait's attribute-resolver
    public function getAttribute($key) {
        if ($result = $this->resolveRatingAttribute($key)) {
            return $result;
        }
        return parent::getAttribute($key);
    }   

}

属性命名约定

为了最大限度地发挥此插件的作用,我们建议您为可评分字段使用后缀 _ratingRating