permafrost-dev/laravel-helpers

此包已被 弃用 且不再维护。未建议替代包。

为Laravel提供有用的辅助函数和类

1.0.0 2020-07-25 10:09 UTC

This package is auto-updated.

Last update: 2021-07-31 22:02:23 UTC


README

描述

安装

您可以通过composer安装此包

composer require permafrost-dev/laravel-helpers

辅助类

  • \Permafrost\Helpers\ModelHelper

示例

  • 检索前10个模型,并将结果缓存30秒。30秒内的重复调用将返回缓存的结果,而不是执行新的数据库查询。
$models = ModelHelper::create(MyClass::class)
    ->cached(30)
    ->orderBy('id')
    ->latest()
    ->get();
  • 检索所有MyClass记录的id值,并将其缓存60秒。这可以在数据库播种期间使用,可以显著加快过程。
// use the model helper class
$ids = ModelHelper::create(MyClass::class)
    ->cached(60)
    ->ids();

// or use a helper function
$ids = get_cached_model_ids(MyClass::class, 60);
  • Permafrost\Helpers\RouteHelper

示例

假设一个定义为 /products/{category}/{id} 且命名为 web.products.show 的路由,返回 /products/books/123

    //class-based
    RouteHelper::routename('web.products.show/books/123');
    
    //helper function
    routename('web.products.show/books/123');

辅助函数

    //get all ids for a model and cache the results; returns cached results if they exist
    function get_cached_model_ids(string $modelClass, int $ttlSeconds, int $recordLimit): array;
    
    //get all values of a column for a model and cache the results; returns cached results if they exist
    function get_cached_model_columns(string $modelClass, string $column, int $ttlSeconds, int $recordLimit): array;

    //get all values of a column for a model
    function get_model_column($modelClass, string $column, int $recordLimit): array;

    //get all ids for a model
    function get_model_ids($modelClass, int $recordLimit): array;

    //truncates text with an ellipsis, but doesn't return partially truncated words
    function str_tease(string $string, int $length, string $moreTextIndicator = '...'): string;

    //returns a relative url instead of a complete url
    function relative_route(string $name, array $parameters): string;

    //returns the value of a named route along with the provided parameters (see RouteHelper class)
    function routepath(string $routepath): string;

    //returns an array of the validated data
    function validate($fields, $rules);
    
    //returns true if the data passes validation
    function validated($fields, $rules);
    
    //creates a validator instance, accepting either array or string parameters
    function validator_create($fields, $rules);

测试

./vendor/bin/phpunit --testdox

许可

MIT许可(MIT)。请参阅LICENSE文件以获取更多信息。