1. Go to this page and download the library: Download maize-tech/laravel-helpers library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
maize-tech / laravel-helpers example snippets
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(),
];
hlp()->sanitizeUrl('mywebsite.com'); // using the helper function
\Maize\Helpers\Helper::sanitizeUrl('mywebsite.com'); // using the static method
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);
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
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
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
);
namespace App\Helpers\Macros;
use Maize\Helpers\HelperMacro;
class Ping implements HelperMacro
{
public function __invoke(): \Closure
{
return function (): string {
return 'pong';
};
}
}
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([
'ping' => \App\Helpers\Macros\Ping::class,
])->toArray(),
];
use Maize\Helpers\Helper;
Helper::macro('ping', fn (): string => 'pong');