tanthammar/laravel-extras
Composer 安装命令:
composer require tanthammar/laravel-extras
包简介
Custom validation rules for the Laravel Framework
README 文档
README
Requirements
- PHP 8.3+
- Laravel v11.0+
- MySQL or PostgreSQL database (for builder macros using raw SQL)
Installation
composer require tanthammar/laravel-extras
Migration Setup
This package publishes a migration file for the ocr_numbers table, used to ensure cross-table uniqueness of OCR numbers.
php artisan vendor:publish --provider="TantHammar\LaravelExtras\LaravelExtrasServiceProvider" --tag="migrations" php artisan migrate
Query Builder Macros
Search and Filter Macros
whereStartsWith / orWhereStartsWith
Case-insensitive prefix matching:
// Find users whose email starts with 'john' User::whereStartsWith('email', 'john')->get(); // Chain with other conditions User::where('active', true)->orWhereStartsWith('name', 'admin')->get();
whereEndsWith / orWhereEndsWith
Case-insensitive suffix matching:
// Find users with gmail addresses User::whereEndsWith('email', 'gmail.com')->get();
whereAllLike
Search multiple columns where ALL must match:
// Find users where BOTH name and email contain 'john' User::whereAllLike(['name', 'email'], 'john')->get();
orWhereAnyLike
Search multiple columns where ANY can match:
// Find users where name OR email contains 'john' User::where('active', true)->orWhereAnyLike(['name', 'email'], 'john')->get();
whereContains / orWhereContains
Space-separated word searching:
// Find users where name contains both 'john' AND 'doe' (in any order) User::whereContains('name', 'john doe')->get(); // OR condition with contains User::where('active', true)->orWhereContains(['name', 'email'], 'john doe')->get();
whereRelationsLike
Search across model relationships:
// Search posts by content, title, or author name Post::whereRelationsLike(['title', 'content', 'author.name'], 'laravel')->get();
Translatable Content Macros
whereTranslatableStartsWith / orWhereTranslatableStartsWith
Search JSON translation columns:
// Find events where localized name starts with 'Summer' Event::whereTranslatableStartsWith('name', 'Summer')->get(); // Search specific locale Event::whereTranslatableStartsWith('name', 'Été', 'fr')->get();
whereTranslatableLike / orWhereTranslatableLike
// Find events where localized name contains 'festival' Event::whereTranslatableLike('name', 'festival')->get();
whereTranslatableContains / orWhereTranslatableContains
// Find events where localized name contains 'music festival' (both words) Event::whereTranslatableContains('name', 'music festival')->get();
Utility Macros
whereOverlaps
Find overlapping date ranges:
// Find bookings that overlap with a specific date range Booking::whereOverlaps('start_date', 'end_date', '2024-01-01', '2024-01-31')->get();
existsById / existsByUuid
Quick existence checks:
// Check if user exists by ID if (User::existsById(123)) { // User exists } // Check if record exists by UUID if (Product::existsByUuid('550e8400-e29b-41d4-a716-446655440000')) { // Product exists }
orderByTranslation / orderByLocale
Locale-aware sorting:
// Sort by translated name field Event::orderByTranslation('name', 'asc')->get(); // Sort by regular field with locale collation User::orderByLocale('name', 'desc')->get();
Helper Classes
String and Text Processing
CleanNumber
Remove all non-numeric characters:
use TantHammar\LaravelExtras\CleanNumber; $phone = CleanNumber::clean('+46 (0)70-123 45 67'); // Result: '46701234567'
NoWhiteSpace
Remove all whitespace characters:
use TantHammar\LaravelExtras\NoWhiteSpace; $clean = NoWhiteSpace::clean("Hello\n\tWorld "); // Result: 'HelloWorld'
MarkdownToHtmlString
Convert Markdown to HTML:
use TantHammar\LaravelExtras\MarkdownToHtmlString; $html = new MarkdownToHtmlString('**Bold text** with [link](https://example.com)'); // Use in Blade templates {!! $html !!} // Filament placeholder field example Placeholder::make(trans('fields.accounting-chart')) ->disableLabel() ->content(new MarkdownToHtmlString(__('fields.account_hint'))) ->columnSpan('full')
Data Manipulation
Array Helper
use TantHammar\LaravelExtras\ArrHelper; // Remove empty values from array $clean = ArrHelper::filledOnly(['name' => 'John', 'email' => '', 'age' => null]); // Result: ['name' => 'John'] // Swap array items (using Arr::swap macro) $assocArray = [ 'item_one' => ['name' => 'One'], 'item_two' => ['name' => 'Two'], 'item_three' => ['name' => 'Three'], 'item_four' => ['name' => 'Four'], ]; $newArray = Arr::swap($assocArray, 'item_one', 'item_three'); /* * Result: * [ * 'item_three' => ['name' => 'Three'], * 'item_two' => ['name' => 'Two'], * 'item_one' => ['name' => 'One'], * 'item_four' => ['name' => 'Four'], * ] */
Money Integer Cast
Store monetary values as integers:
use TantHammar\LaravelExtras\MoneyIntegerCast; class Product extends Model { protected $casts = [ 'price' => MoneyIntegerCast::class, ]; } // Store $19.99 as 1999 (integer) $product = new Product(); $product->price = 19.99; // Stored as 1999 echo $product->price; // Outputs: 19.99
Specialized Utilities
OCR3 (Swedish Bank References)
Generate OCR numbers with Luhn algorithm:
use TantHammar\LaravelExtras\OCR3; // Generate unique OCR number $ocr = OCR3::unique(); // Result: '1234567890' (with valid check digit) // Generate with specific length $ocr = OCR3::unique(8); // Result: '12345678' (8 digits with check digit) // Use in factories $factory->define(Invoice::class, function (Faker $faker) { return [ 'ocr_number' => $faker->ocr3(), ]; });
SKU Generator
Generate product SKUs:
use TantHammar\LaravelExtras\SKU; $sku = SKU::generate('Premium Widget'); // Result: 'PW-ABC123' (source prefix + random signature)
Debug Utilities
PrettyPrint
Formatted debugging output:
use TantHammar\LaravelExtras\PrettyPrint; // In PHP PrettyPrint::dump($complexArray); // In Blade templates @prettyPrint($data)
Breaking Changes from Previous Versions
Query Macro Renames (v4.0+)
whereLike()→whereAllLike()orWhereLike()→orWhereAnyLike()
Version Requirements (v4.0+)
- Minimum PHP version: 8.3+ (was 8.1+)
- Minimum Laravel version: 11.0+ (was 9.0+)
Implementation Changes (v4.0+)
- All query macros now use Laravel's native
whereLike()method withcaseSensitive: false - Removed custom database operator detection in favor of Laravel's built-in handling
- Improved query logic for proper OR condition chaining
tanthammar/laravel-extras 适用场景与选型建议
tanthammar/laravel-extras 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.62k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 11 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validation」 「rules」 「laravel」 「tanthammar」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tanthammar/laravel-extras 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tanthammar/laravel-extras 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tanthammar/laravel-extras 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Magento 2 module which adds support for Speculation Rules API for instant page loads.
The coding standard applying to all Youdot PHP projects, based on Doctrine set of PHPCS rules, with additional checks.
Business rules engine tools.
A set of Laravel validation rules
Adds request-parameter validation to the SLIM 3.x PHP framework
The last validation library you will ever need!
统计信息
- 总下载量: 1.62k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 10
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-11-17