codewithkyrian/laravel_position

一个简单的可扩展的Laravel集合宏,用于评估集合中项目的位置或排名,并将位置追加到每个项中,使用您选择的键。

1.0.1 2022-01-11 23:24 UTC

This package is auto-updated.

Last update: 2024-09-12 05:29:36 UTC


README

Latest Version on Packagist GitHub Code Style Action Status Total Downloads

一个简单的可扩展的Laravel集合宏,用于评估集合中项目的位置或排名,并将位置追加到每个项中,使用您选择的键。默认键是'position'。

安装

您可以通过composer安装此包

composer require codewithkyrian/laravel_position

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="position-config"

这是发布配置文件的内容

return [
    /**
     * The default key to use when adding the position to each item
     * in the collection. It could still be overridden when calling
     * method by passing a string as the second argument.
     */
    'key' => 'position',

    /**
     * Whether to show the total number of items in the collection
     * in the position text eg. 12th out of 30
     */
    'show_total' => false,

    /**
     * The text to join the position and the total number of items
     * in the collection
     */
    'join_text' => 'out of'
];

现在您已经完成了。您现在可以在项目的任何地方使用此宏。

用法

要使用此宏,您需要在集合实例上调用rankBy方法,并将用于排名的键作为第一个参数传递。此包可用于包含数组或对象的集合。

$items = collect(
    [
        [ 'name' => 'Mr A', 'score' => 100 ],
        [ 'name' => 'Mr B', 'score' => 74 ],
        [ 'name' => 'Mr C', 'score' => 60 ],
        [ 'name' => 'Mr D', 'score' => 83 ],
        [ 'name' => 'Mr E', 'score' => 89 ],
    ]
);
$rankedItems = $items->rankBy('score');
dd($rankedItems->toArray());

/*
array:5 [▼
  0 => array:3 [▼
    "name" => "Mr A"
    "score" => 100
    "position" => "1st"
  ]
  1 => array:3 [▼
    "name" => "Mr E"
    "score" => 89
    "position" => "3rd"
  ]
  2 => array:3 [▼
    "name" => "Mr D"
    "score" => 83
    "position" => "4th"
  ]
  3 => array:3 [▼
    "name" => "Mr B"
    "score" => 74
    "position" => "5th"
  ]
  4 => array:3 [▼
    "name" => "Mr C"
    "score" => 60
    "position" => "6th"
  ]
]
*/

您还可以传递复合键,使用点表示法形式,用于具有嵌套值的集合。

$users = collect(
    [
        [ 
            'name' => 'Mr A', 
            'result' => [
                'score' => 100, 'status' => 1
            ]
        ],
        [ 
            'name' => 'Mr B', 
            'result' => [
                'score' => 74, 'status' => 1
            ]
        ],
        [ 
            'name' => 'Mr C', 
            'result' => [
                'score' => 60, 'status' => 1
            ]
        ],
        [ 
            'name' => 'Mr D', 
            'result' => [
                'score' => 83, 'status' => 1
            ]
        ]
    ]
);
$rankedUsers = $users->rankBy('result.score');
echo ($rankedUsers->toArray());

/*
array:4 [▼
  0 => array:3 [▼
    "name" => "Mr A"
    "result" => array:2 [▼
      "score" => 100
      "status" => 1
    ]
    "position" => "1st"
  ]
  1 => array:3 [▼
    "name" => "Mr D"
    "result" => array:2 [▼
      "score" => 83
      "status" => 1
    ]
    "position" => "3rd"
  ]
  2 => array:3 [▼
    "name" => "Mr B"
    "result" => array:2 [▼
      "score" => 74
      "status" => 1
    ]
    "position" => "4th"
  ]
  3 => array:3 [▼
    "name" => "Mr C"
    "result" => array:2 [▼
      "score" => 60
      "status" => 1
    ]
    "position" => "5th"
  ]
]
*/

为了进一步自定义宏的行为,您可以将第二个参数传递以控制输出位置到项上使用的键。默认情况下,它是position。请记住,您也可以通过包的配置文件中的key键来更改此设置。

$rankedItems = $items->rankBy('score', 'class_position');
echo ($rankedItems->toArray());

/*
array:5 [▼
  0 => array:3 [▼
    "name" => "Mr A"
    "score" => 100
    "class_position" => "1st"
  ]
  1 => array:3 [▼
    "name" => "Mr E"
    "score" => 89
    "class_position" => "3rd"
  ]
  2 => array:3 [▼
    "name" => "Mr D"
    "score" => 83
    "class_position" => "4th"
  ]
  3 => array:3 [▼
    "name" => "Mr B"
    "score" => 74
    "class_position" => "5th"
  ]
  4 => array:3 [▼
    "name" => "Mr C"
    "score" => 60
    "class_position" => "6th"
  ]
]
*/

更新日志

有关最近更改的更多信息,请参阅更新日志

贡献

有关详细信息,请参阅贡献指南

安全漏洞

有关如何报告安全漏洞的详细信息,请参阅我们的安全策略

致谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件