Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Helper Laravel Package

kominfogusit/laravel-helper

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require kominfogusit/laravel-helper
    

    Publish the config (if needed):

    php artisan vendor:publish --provider="KominfoGusit\Helper\HelperServiceProvider"
    
  2. First Use Case Quickly generate a UUID:

    use KominfoGusit\Helper\Facades\Helper;
    
    $uuid = Helper::uuid(); // Returns a v4 UUID string
    
  3. Where to Look First

    • Facade: KominfoGusit\Helper\Facades\Helper (primary entry point).
    • Config: config/helper.php (if published).
    • Documentation: Check the package’s README.md for core utilities (e.g., slugify(), randomString(), array_diff_recursive()).

Implementation Patterns

Core Workflows

  1. String Manipulation

    // Slugify a string
    $slug = Helper::slugify('Hello World!'); // "hello-world"
    
    // Random string generation
    $token = Helper::randomString(16); // e.g., "aB3x9KpL2qR7tY1"
    
  2. Array Utilities

    // Deep array diff (returns associative array of differences)
    $diff = Helper::arrayDiffRecursive(
        ['a' => 1, 'b' => ['x' => 10]],
        ['a' => 1, 'b' => ['x' => 20]]
    );
    
    // Merge arrays with custom logic
    $merged = Helper::arrayMergeRecursiveDistinct($array1, $array2);
    
  3. Data Validation & Sanitization

    // Sanitize input (e.g., remove HTML tags)
    $cleanInput = Helper::sanitize($userInput);
    
    // Check if a string is a valid email
    $isValid = Helper::isEmail('[email protected]');
    
  4. File & Path Handling

    // Generate a unique filename
    $filename = Helper::uniqueFilename('image', 'jpg');
    
    // Get the public path
    $path = Helper::publicPath('assets/images');
    
  5. Date/Time Helpers

    // Format a date (e.g., for Indonesian locale)
    $formatted = Helper::formatDate('2023-10-01', 'd-m-Y');
    
    // Check if a date is within a range
    $isInRange = Helper::isDateInRange('2023-10-01', '2023-01-01', '2023-12-31');
    
  6. Integration with Laravel Features

    • Blade Directives:
      // Register in a service provider
      Blade::directive('uuid', fn() => "<?php echo \\KominfoGusit\\Helper\Facades\\Helper::uuid(); ?>");
      
      // Usage in Blade:
      <div>{{-- @uuid --}}</div>
      
    • Form Requests:
      use KominfoGusit\Helper\Facades\Helper;
      
      public function rules()
      {
          return [
              'email' => ['required', 'email', fn($attr, $value) =>
                  Helper::isValidEmail($value) ? 'valid' : 'invalid'
              ],
          ];
      }
      
  7. Testing

    // Mock the Helper facade in tests
    Helper::shouldReceive('slugify')->andReturn('test-slug');
    

Gotchas and Tips

Pitfalls

  1. Locale-Specific Functions

    • Some helpers (e.g., formatDate()) may rely on the app’s locale. Ensure App::setLocale() is set correctly if using non-default locales.
    • Fix: Explicitly pass a locale:
      Helper::formatDate('2023-10-01', 'd-m-Y', 'id');
      
  2. UUID Version Confusion

    • The package defaults to UUIDv4. If you need UUIDv1 (timestamp-based), check if the package supports it or implement a custom solution.
    • Tip: Extend the facade:
      Helper::extend('uuidv1', fn() => Str::uuid('v1'));
      
  3. Array Merge Overwrites

    • arrayMergeRecursiveDistinct may not handle all edge cases (e.g., nested arrays with mixed types). Test with complex data structures.
    • Workaround: Use array_merge_recursive with custom logic if needed.
  4. Sanitization Edge Cases

    • sanitize() may strip critical characters (e.g., in code snippets). Use sparingly for user-generated content.
    • Tip: Combine with strip_tags() or allowlists:
      $safeHtml = Helper::sanitize($input, ['<b>', '<i>']);
      
  5. Public Path Caching

    • publicPath() caches the base path. Clear the cache if paths change dynamically (e.g., in multi-tenant apps).
    • Fix: Use Helper::refreshPublicPathCache().
  6. Performance with Large Arrays

    • arrayDiffRecursive() can be slow for very large arrays. For production, consider batch processing or alternative approaches.

Debugging Tips

  1. Enable Debug Mode Add this to config/helper.php to log helper calls:

    'debug' => env('APP_DEBUG', false),
    
  2. Check for Deprecated Methods The package may evolve. Use php artisan vendor:list --installed to track updates and check the changelog.

  3. Override Default Behavior Bind custom implementations in the service provider:

    $this->app->bind('helper', fn($app) => new CustomHelper());
    

Extension Points

  1. Add Custom Helpers Extend the Helper facade dynamically:

    Helper::extend('customHelper', fn($arg) => $arg * 2);
    // Usage: Helper::customHelper(5); // Returns 10
    
  2. Create a Helper Trait For reusable logic across services:

    use KominfoGusit\Helper\Traits\HelperTrait;
    
    class MyService {
        use HelperTrait;
    
        public function doSomething() {
            $slug = $this->slugify('Test');
        }
    }
    
  3. Localization Hooks Override locale-specific helpers in a service provider:

    Helper::macro('formatDate', function($date, $format, $locale = null) {
        // Custom logic here
    });
    
  4. Event Listeners for Helper Calls Log or monitor helper usage via events (if the package supports it). Example:

    // Hypothetical event (check package docs)
    event(new \KominfoGusit\Helper\Events\HelperCalled($helperName, $args));
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
abbasudo/artisan-route
mirvan/ippanel
ignibyte/aic-framework
glavweb/data-schema-bundle
glavweb/datagrid-bundle
tacticmedia/scim-server
uni-method/frame
neika-ai/sdk
adachsoft/php-code-style
pharaonic/laravel-notification-dependencies
beautinow/translug
dev-brainiac/bot-cloaked
octoberrain/html
octoberrain/extension
octoberrain/exception
octoberrain/contracts
adachsoft/api-version-bundle
cgv/localized-resource
noith/settings
kda/laravel-package