mcmatters/laravel-helpers

Laravel 辅助函数

v3.1.1 2023-11-13 07:17 UTC

README

Latest Stable Version Build Status Total Downloads

安装

composer require mcmatters/laravel-helpers

然后运行以下命令

php artisan vendor:publish --provider="\McMatters\Helpers\ServiceProvider"

如果您喜欢手动安装,则可以将以下内容添加到 config/app.php 文件的 providers 部分下一行

'providers' => [
    McMatters\Helpers\ServiceProvider::class,
],

否则,它将自动发现。

配置

v2.0 版本开始,您可以禁用所有辅助函数并使用辅助类,如果您更喜欢使用辅助函数,可以选择您想使用的确切部分(例如 arraydevstring 等)

函数

数组

array_first_key(array $array)

array_first_key 函数从给定的数组中返回第一个键。

array_has_with_wildcard(array $array, string $keys, bool $searchWithSegment)

array_has_with_wildcard 函数类似于 array_has 函数,具有使用通配符搜索的能力。它还可以搜索带有点(即当验证失败时从多个字段获取数据)的键。

array_key_by(array $array, string $key)

array_key_by 函数返回一个按 $key 分组的关联数组。

array_contains(array $array, string $needle, bool $byKey = false)

array_contains 函数如果数组包含给定的 $needle,则返回 true。您还可以通过数组键进行搜索。此函数仅在数组的第一级中搜索。

array_has_only_int_keys(array $array)

array_has_only_int_keys 函数检查给定数组的键是否为整数。

shuffle_assoc(array $array)

shuffle_assoc 函数类似于基本的 shuffle 函数,但保留了键。

array_change_key_case_recursive(array $array, int $case = CASE_LOWER)

array_change_key_case_recursive 函数类似于 array_change_key_case 函数,但它递归工作。

Artisan

get_php_path()

get_php_path 函数返回可执行 php 的路径。

get_artisan()

get_artisan 函数返回可执行 artisan 的路径。

run_background_command(string $command, array $parameters = [])

run_background_command 函数在后台运行给定的命令。

get_class_constants($class)

get_class_constants 函数返回一个包含所有类常量的数组。

get_class_constants_start_with($class, string $string)

get_class_constants_start_with 函数返回一个包含以传入字符串开始的常量的数组。

数据库

compile_sql_query($sql, array $bindings = null)

compile_sql_query 函数替换所有占位符和给定的绑定。

get_all_tables(bool $withColumns = true, string $connection = null)

get_all_tables 函数返回包含其列的所有表的列表。

search_entire_database(string $keyword, string $connection = null)

search_entire_database 函数搜索整个数据库。它仅适用于 MySQL 和 SQL 驱动程序。

has_query_join_with($query, string $with)

has_query_join_with 函数确定查询是否有连接的表。

开发

ddq($query, bool $die = false)

ddq 函数类似于 dd 函数,但仅用于数据库查询。

dump($value, bool $output = false)

dump 函数类似于 dd 函数,但不停止 PHP 脚本执行。

环境

is_production_environment()

《is_production_environment》函数用于检查是否正在使用生产环境。

is_local_environment()

《is_local_environment》函数用于检查是否正在使用本地环境。

is_testing_environment()

《is_testing_environment》函数用于检查是否正在使用测试环境。

数学

calculate_percentage($count, $total, int $decimals)

《calculate_percentage》函数用于计算百分比。

calculate_discount($discount, $total, int $decimals)

《calculate_discount》函数用于计算折扣。

calculate_with_discount($discount, $total, int $decimals)

《calculate_with_discount》函数用于计算带折扣的价格。

has_float_remainder(float $number)

《has_float_remainder》函数用于检查浮点数的余数是否不为零。

convert_bytes($value, string $returnType)

《convert_bytes》函数用于在任意大小之间转换字节。

get_size_types()

《get_size_types》函数返回所有可用的尺寸类型列表。

is_number_even($number)

《is_number_even》函数用于检查数字是否为偶数。

is_number_odd($number)

《is_number_odd》函数用于检查数字是否为奇数。

is_number_in_range($number, $from, $to)

《is_number_in_range》函数用于检查数字是否在给定的范围内。

模型

get_model_from_query($query)

《get_model_from_query》函数从查询返回一个模型。

destroy_models_from_query($query)

《destroy_models_from_query》函数销毁查询中的所有模型。当您想删除多个模型并调用模型的钩子时非常有用。

is_morphed_belongs_parent(Model $morphed, Model $parent, string $name, string $type, string $id)

《is_morphed_belongs_parent》函数确定修改后的模型是否属于父模型。

请求

is_request_method_update($request)

《is_request_method_update》函数用于检查当前请求方法是否用于更新。

服务器

long_processes()

《long_processes》函数设置服务器执行长时间操作的最佳配置的最大值。

get_upload_max_filesize(string $returnType)

《get_upload_max_filesize》函数返回任何格式中upload_max_filesize的值。

get_post_max_size(string $returnType)

《get_post_max_size》函数返回任何格式中post_max_size的值。

is_max_post_size_exceeded()

《is_max_post_size_exceeded》函数检查是否超过了post_max_size的允许大小。

get_max_response_code()

《get_max_response_code》函数返回响应代码的最大可用值。

字符串

str_lower(string $string)

《str_lower》是Str::lower的功能包装。

str_upper(string $string)

《str_upper》是Str::upper的功能包装。

str_ucwords(string $string)

《str_ucwords》函数将值转换为带空格的studly caps格式。

strpos_all(string $haystack, string $needle, bool $caseInsensitive = false)

《strpos_all》函数返回$haystack中所有$needle的实例。

类型

random_bool()

《random_bool》函数返回一个随机的布尔值。

casting_bool($value, bool $default)

《casting_bool》函数返回转换后的布尔值。

is_json($json, $returnDecoded)

《is_json》函数检查传入的字符串是否是JSON。

is_uuid($string)

《is_uuid》函数检查传入的值是否是有效的uuid值。

URL

get_base_url(string $url)

《get_base_url》函数返回基本URL。

get_host_url(string $url, bool $stripWww = true)

《get_host_url》函数返回主机URL。

routes_path(string $path)

《routes_path》函数返回到routes文件夹的路径。