tyler36 / laravel-helper-macros by tyler36

Collection of Helpers and macros for Laravel
82
3
1
Package Data
Maintainer Username: tyler36
Package Create Date: 2016-07-11
Package Last Update: 2022-03-28
Language: PHP
License: Unknown
Last Refreshed: 2025-02-09 15:23:22
Package Statistics
Total Downloads: 82
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 3
Total Watchers: 1
Total Forks: 1
Total Open Issues: 0

Laravel Helpers

Installation

1.Install package through composer.

   composer require tyler36/laravel-helper-macros

2.Add package to config/app.php

    Tyler36\laravelHelpers\LaravelHelperServiceProvider::class,

Helpers

Validator

Inspired by Validate (almost) anything in Laravel. Uses Laravel Validator class to validate anything.

EG. Validate Date (expects FALSE):

    tyler36\laravelHelpers\Helper::validate('20150230', 'date');

EG. Validate Date (expects TRUE):

    tyler36\laravelHelpers\Helper::validate('20150230', 'date');

Macros

Collections

These macros extend collection functionality.

pipe

Inspired by The pipe collection macro. Pass collection to a function.

$collect = collect([1, 2, 3])
    ->pipe(function ($data) {
        return $data->merge([4, 5, 6]);
    });

dd

Inspired by Debugging collections. Debug a collection by 'dump and die' the current state. Can be used mid-chain to break and dump.

collect($items)
  ->filter(function() {
     ...
   })
  ->dd()

ifEmpty

Run callback if collection is empty.

$items = collect([]);
$items->ifEmpty(function(){
    abort(500, 'No items');
});

ifAny

Run callback if collection has data ( >1 item).

$errors = collect(['Something went wrong']);
$errors->ifAny(function(){
    abort(500, 'There was at-least 1 problem');
});

mapToAssoc

Convert collection to associative array Inspired by Customizing Keys When Mapping Collections

$emailLookup = $employees->mapToAssoc(function ($employee) {
    return [$employee['email'], $employee['name']];
});

Response

These macros help to standardize api json response returns throughout the application. Inspired by Laravel Response Macros for APIs

Success

Returns payload ($data) with a HTTP status code 200

response()->success($data);

EG.

response()->success(['earth' => 3, 'sun' => 'yellow'])

Returns HTTP status 200 with the following:

{"errors":false,"data":{"earth":3,"sun":"yellow"}}

noContent

Returns empty content with a HTTP status code 402

response()->noContent()

Error

Returns message ($message) content with an optional HTTP status code ($statusCode) [Default: HTTP status code 400]

response()->error($message);

Eg.

response()->error('There was an error.');

Returns HTTP status 400 with the following:

{"errors":true,"message":"There was an error."}

Eg.

response()->error('Not authorized!', 403);

Returns HTTP status 403 with the following:

{"errors":true,"message":"Not authorized!"}