PHPackages                             maize-tech/laravel-helpers - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. maize-tech/laravel-helpers

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

maize-tech/laravel-helpers
==========================

Laravel Helpers

4.3.1(2y ago)302.0k7[4 PRs](https://github.com/maize-tech/laravel-helpers/pulls)MITPHPPHP ^8.1CI passing

Since Jan 18Pushed 1mo ago4 watchersCompare

[ Source](https://github.com/maize-tech/laravel-helpers)[ Packagist](https://packagist.org/packages/maize-tech/laravel-helpers)[ Docs](https://github.com/maize-tech/laravel-helpers)[ GitHub Sponsors](https://github.com/maize-tech)[ RSS](/packages/maize-tech-laravel-helpers/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (12)Versions (14)Used By (0)

Laravel Helpers
===============

[](#laravel-helpers)

[![Latest Version on Packagist](https://camo.githubusercontent.com/359072962717efde09232c44bd7666ea90a638fdf1a01c0aeee36835bc127d73/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d61697a652d746563682f6c61726176656c2d68656c706572732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/maize-tech/laravel-helpers)[![GitHub Tests Action Status](https://camo.githubusercontent.com/0398ed24046d8498dcb95daf0a54d70d150977bad5bbae35e27d9194a15259e3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d61697a652d746563682f6c61726176656c2d68656c706572732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/maize-tech/laravel-helpers/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/e3c13d199e39e87c3c5c78dfcc131677fa034d193098468a1bae0c374917b2fc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d61697a652d746563682f6c61726176656c2d68656c706572732f7068702d63732d66697865722e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/maize-tech/laravel-helpers/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/fd271d32389c731eb698c51ee0b5abb107de8df6326a13fe68c26d90bba9fea6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d61697a652d746563682f6c61726176656c2d68656c706572732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/maize-tech/laravel-helpers)

This repository contains some useful helpers for most applications using Laravel.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require maize-tech/laravel-helpers
```

You can publish the config file with:

```
php artisan vendor:publish --tag="helpers-config"
```

This is the content of the published config file:

```
return [

    /*
    |--------------------------------------------------------------------------
    | Helper macros
    |--------------------------------------------------------------------------
    |
    | Here you may specify the full list of helper macros which will automatically
    | be registered on boot.
    | The key defines the method name, whereas the value should be the
    | fully qualified name of the invokable class.
    |
    */

    'macros' => Helper::defaultMacros()->merge([
        // 'methodName' => App\Example\ExampleClass::class,
    ])->toArray(),

];
```

Usage
-----

[](#usage)

To use the package, you can simply call the `hlp()` helper function, followed by one of the [Available methods](#available-methods) methods listed below. If needed, you could also call the static method directly.

Here's an example using both the helper function and the static method:

```
hlp()->sanitizeUrl('mywebsite.com'); // using  the helper function

\Maize\Helpers\Helper::sanitizeUrl('mywebsite.com'); // using the static method
```

Available methods
-----------------

[](#available-methods)

- [`anonymizeFilename`](#anonymizefilename)
- [`classUsesTrait`](#classusestrait)
- [`instanceofTypes`](#instanceoftypes)
- [`isUrl`](#isurl)
- [`modelKeyName`](#modelkeyname)
- [`morphClassOf`](#morphclassof)
- [`resolveMorphedModel`](#resolvemorphedmodel)
- [`paginationLimit`](#paginationlimit)
- [`pipe`](#pipe)
- [`sanitizeArrayOfStrings`](#sanitizearrayofstrings)
- [`sanitizeString`](#sanitizestring)
- [`sanitizeUrl`](#sanitizeurl)

### `anonymizeFilename`

[](#anonymizefilename)

The `anonymizeFilename` function returns a randomized name of the given file followed by its extension.

```
string $filename = 'my-custom-file.xml';

// returns a UUID string followed by the file extension
// e.g. 'd437fd98-68d1-4874-b0e7-fac06e587083.xml'
hlp()->anonymizeFilename($filename);
```

### `classUsesTrait`

[](#classusestrait)

The `classUsesTrait` function returns whether a class object or name uses the given trait or not.

```
use App\Models\User;
use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;

$model = User::firstOrFail();

hlp()->classUsesTrait(HasFactory::class, $model); // returns true

hlp()->classUsesTrait(HasFactory::class, User::class); // returns true

hlp()->classUsesTrait(Exception::class, $model); // returns false

hlp()->classUsesTrait(Exception::class, User::class); // returns false
```

### `instanceofTypes`

[](#instanceoftypes)

The `instanceofTypes` function returns whether a class object is an instance of at least one of the given types or not.

```
use App\Models\User;
use Exception;
use Illuminate\Database\Eloquent\Model;

$model = User::firstOrFail();

hlp()->instanceofTypes($model, Model::class); // returns true

hlp()->instanceofTypes($model, Exception); // returns false

hlp()->instanceofTypes($model, [
    Model::class,
    Exception::class,
]); // returns true
```

### `isUrl`

[](#isurl)

The `isUrl` function returns whether a string is a valid URL or not.

```
hlp()->isUrl('https://my-application.test'); // returns true

hlp()->isUrl('not-an-url'); // returns false
```

### `modelKeyName`

[](#modelkeyname)

The `modelKeyName` function returns the key name of a given model object or class name.

```
use App\Models\User;

$model = User::firstOrFail();

hlp()->modelKeyName($model); // returns 'id'

hlp()->modelKeyName(User::class); // returns 'id'
```

### `morphClassOf`

[](#morphclassof)

The `morphClassOf` function returns the morph class name of a given model object or class name.

```
use App\Models\User;
use Illuminate\Database\Eloquent\Relations\Relation;

$model = User::firstOrFail();

hlp()->morphClassOf($model); // returns 'App\Models\User'

hlp()->morphClassOf(User::class); // returns 'App\Models\User'

Relation::enforceMorphMap([
    'user' => User::class,
]);

hlp()->morphClassOf($model); // returns 'user'

hlp()->morphClassOf(User::class); // returns 'user'
```

### `resolveMorphedModel`

[](#resolvemorphedmodel)

The `resolveMorphedModel` function returns the fully qualified model class name of a given morph class name.

You can either pass the singular or plural name of the morph class name.

```
hlp()->resolveMorphedModel('users'); // returns 'App\Models\User'

hlp()->resolveMorphedModel('user'); // returns 'App\Models\User'
```

### `paginationLimit`

[](#paginationlimit)

The `paginationLimit` function returns the amount of items per page.

It is useful when working with queries who need a pagination, and allows to define a default pagination limit and the max amount of items per page.

It will also check whether the request's query string contains a `limit` parameter: if true, the given limit overrides the default limit.

```
use App\Models\Article;

// use the default pagination limit (16 items)
// GET /api/articles
Article::paginate(
    hlp()->paginationLimit() // returns 16 items
);

// use the pagination limit given by the request query string
// GET /api/articles?limit=20
Article::paginate(
    hlp()->paginationLimit() // returns 20 items
);

// provide a custom default pagination limit
// GET /api/articles
Article::paginate(
    hlp()->paginationLimit(30) // returns 30 items
);

// when defined, the request query string limit overrides the default limit
// GET /api/articles?limit=20
Article::paginate(
    hlp()->paginationLimit(30) // returns 20 items
);

// provide a max limit of items for each page
// GET /api/articles?limit=200
Article::paginate(
    hlp()->paginationLimit(16, 50) // returns 50 items
);
```

### `pipe`

[](#pipe)

The `pipe` function allows you to integrate Laravel pipelines with ease. The first parameter is the object to be sent through the pipeline, while the second is the list of pipes.

```
hlp()->pipe('test', [
    \Maize\Helpers\Tests\Support\Actions\Uppercase::class,
]); // returns 'TEST'

hlp()->pipe('test', [
    \Maize\Helpers\Tests\Support\Actions\Uppercase::class,
    \Maize\Helpers\Tests\Support\Actions\Reverse::class,
]); // returns 'TSET'

hlp()->pipe('', []) // returns an empty string
```

### `sanitizeArrayOfStrings`

[](#sanitizearrayofstrings)

The `sanitizeArrayOfStrings` function sanitizes an array of strings by removing all HTML tags and whitespace from both ends. When passing an associative array, it also filters all keys with an empty value.

```
hlp()->sanitizeArrayOfStrings(['   test   ', '   test   ']); // returns '['test', 'test']'

hlp()->sanitizeArrayOfStrings(['a' => '   test      ', 'b' => '   test      ']) // returns ['a' => 'test', 'b' => 'test']

hlp()->sanitizeArrayOfStrings(['a' => '']); // returns an empty array
```

### `sanitizeString`

[](#sanitizestring)

The `sanitizeString` function sanitizes a string by removing all HTML tags and whitespace from both ends.

```
hlp()->sanitizeString('   test   '); // returns 'test'

hlp()->sanitizeString('      test   '); // returns 'test'

hlp()->sanitizeString('') // returns an empty string
```

### `sanitizeUrl`

[](#sanitizeurl)

The `sanitizeUrl` function prepends the specified url with the `https` protocol if none is set.

```
hlp()->sanitizeUrl('http://innovation.h-farm.com'); // returns 'http://innovation.h-farm.com'

hlp()->sanitizeUrl('innovation.h-farm.com'); // returns 'https://innovation.h-farm.com'

hlp()->sanitizeUrl('') // returns an empty string
```

Adding custom helper methods
----------------------------

[](#adding-custom-helper-methods)

If needed, you can easily add your own helper methods.

All you have to do is define your custom helper class and implement the `HelperMacro` interface:

```
