Supercharge your Laravel projects with more than 25 useful global helpers.
- Laravel 8.x or later
- PHP 8.0 or later
You can install the package via composer:
composer require darkghosthunter/larahelp
This package is focused on the backend. If you want views helpers, I recommend you to use custom Blade directives instead.
This package includes helpful global helpers for your project make almost anything into beautiful one liners:
Executes a callable using the application Container.
$result = app_call('\App\MyService\Service@createSomething');
Calls a dynamic method or macro if it exists in the object instance. It supports macros and static methods.
class Something {
public function foo() {
return 'bar';
}
}
call_existing(new Something, 'bar'); // null
call_existing(new Something, 'foo'); // "bar"
Return an HTTP 201 response (OK, Created), with optional content.
public function store(Request $request): Response
{
// ...
return created([$post->getKeyName() => $post->getKey()];
}
Updates an item of an array or object using a callback that receives it.
$array = [
'foo' => [
'bar' => null
]
];
data_update($array, 'foo.bar', function ($value) {
if ($value === null) {
return 'baz';
}
})
// [
// 'foo' => [
// 'bar' => 'baz'
// ]
// ];
The value will be updated regardless if the key doesn't exists.
Returns the values of the array, so these can be listed into variables. It accepts an optional offset to remove a given number of first keys.
$array = [
'foo' => 'bar',
'baz' => 'qux',
'quux' => 'quuz',
]
[$baz, $quux] = delist($array, 1);
Returns the difference between two dates in seconds or any other given unit.
$seconds = diff('now', '15th may');
$minutes = diff('today', now())
Transforms a relative path into dot notation.
$path = dot_path('files/user_id_312/videos/');
// files.user_id_312.videos
This does not validate file paths. You should append the filename to the dot-path manually.
Wraps a value or callable into a Closure, if it's not already callable.
$enclosed = enclose('foo');
$enclosed();
// "foo"
Returns the local Filesystem helper, or a list of files in a path.
$content = files()->get('text.txt');
Checks recursively if the object is using a single trait.
trait Child {
// ..
}
trait Parent {
use Child;
// ..
}
class Foo {
use Parent;
}
has_trait(Foo::class, Child::class);
// true
Creates a small BASE64 encoded MD5 hash from a string for portable checksum.
This is very useful to hash large walls of texts, or even files, while compressing the 128-bit hash into a 24-character string.
$hash = hashy('This is a hashable string');
// "TJYa8 63dRbdN6w44shX1g=="
You can use the same function to compare the hashable string with a hash to note if it was modified.
hashy('This is a hashable string', 'TJYa8 63dRbdN6w44shX1g==');
// true
hashy('This is a hashable string!', 'TJYa8 63dRbdN6w44shX1g==');
// false
Check if the application is running in console, like when using Artisan or PHPUnit.
if (in_console()) {
return "We're in console";
}
It also accepts a callable that will be executed if the condition is true.
$result = in_console(fn() => 'foo');
// "foo"
Check if the application is running in development environments: dev
, development
or local
if (in_development()) {
return "Do anything, it doesn't matter!";
}
It also accepts a callable that will be executed if the condition is true.
$result = in_development(fn() => 'foo');
// "foo"
Executes a single callback while the user is logged in.
It basically logs in and logs out an user while executing the callback, which can be useful to do on guest routes.
use App\Models\User;
$user = User::find(1);
$post = logged_in($user, function (User $user) {
return $user->post()->create([
// ..
]);
});
It will throw an exception if there is already a user logged in.
Returns a collection of all public methods from a given class or object.
use Illuminate\Support\Collection;
$methods = methods_of(Collection::class)
return $methods->has('make');
// true
Checks recursively if the object is not using a trait.
trait Parent {
// ..
}
class Foo {
use Parent;
}
missing_trait(Foo::class, Child::class);
// true
Checks if none of the options compared to a subject, or called with it, returns something truthy.
$subject = 'foo';
none_of('foo', ['bar', 'baz', 'qux']);
// false
Using a callable, it will receive the subject and the key being compared.
$subject = 'foo';
none_of('foo', ['bar', 'baz', 'qux'], fn ($subject, $compared) => $subject === $compared);
// false
Assigns an array of values to an object, recursively, using dot notation.
$object = new stdClass();
object_assign($object, ['foo' => 'bar']);
echo $object->foo; // "bar"
Returns an HTTP 204 response (OK, No Content).
public function send(Request $request)
{
// ...
return ok();
}
Returns the period of a given start and end or interval. Periods are extremely useful to get multiple dates based on a start, end, a given time between each occurrence.
The most common way to create a period is defining the start, the number of occurrences, and the interval amount.
$periods = period('now', 4, '15 minutes');
Alternatively, you can specify a start, and end, and the interval of time until that end.
$period = period('today', 'last day of this month', '3 days');
Once you do, you can iterate each moment of time with a foreach
loop.
foreach (period('today', 'last day of this month', '3 days') as $datetime) {
echo $datetime->toDateTimeString();
}
// 2015-07-21 00:00:00
// 2015-07-24 00:00:00
// 2015-07-27 00:00:00
// 2015-07-30 00:00:00
Returns the next or previous period from given date, exclusive by default.
This is very handy to know when the next or previous moment from a given datetime, like the current time.
$period = period('2015-07-06 00:00:00', '2015-07-26 00:00:00', '1 week');
period_from($period, '2015-07-13');
// 2015-07-13
The function can also check the previous date by setting $after
to false
.
$period = period('2015-07-06 00:00:00', '2015-07-26 00:00:00', '1 week');
period_from($period, '2015-07-06');
// 2015-07-13
Dates compared are exclusive. In include the date as inclusive, set
$inclusive
totrue
.
Sends an object through a pipeline.
pipe(10, [
fn($integer, $next) => $next($integer 10);
fn($integer, $next) => $next($integer - 5);
])
// 15
Retrieves an item from the cache, or stores a default value if the item doesn't exist.
remember('foo', 60, function() {
return 'bar';
})
If no ttl
is set, and rather a callback is issued as second parameter, it will be stored forever.
remember('foo', function () {
return 'bar';
})
It supports atomic locks, which are created using the same name key. It will lock the key by a given seconds, while also waiting for the same amount of time.
remember('foo', 60, function() {
return 'bar';
}, 20);
This can be useful to avoid cache data-races, where multiple processes run the same callback because the cache key is not filled yet.
Determine whether the current route's name matches the given patterns.
if (route_is('dahsboard.*')) {
return 'You are in the dashboard';
}
Calls a method on an object if it exists, or returns false. It supports Macros.
if ($rendered = shadow($mayRender, 'render')) {
return $rendered;
}
return response((string)$mayRender);
Runs a callback while sleeping between multiple executions, returning a Collection of all results.
use App\Models\User;
sleep_between(3, 1000, fn() => User::query()->inRandomOrder()->value('name'))
// [
// 'john',
// 'michel',
// 'maria',
// ]
Call the given Closure with the given value then return the value, twice.
use App\Models\User;
use App\Notifications\Message;
return taptap(User::find(1))->notify(new Message('Hello!'))->save();
Transforms a path from dot notation to a relative path.
$path = undot_path('files.user_id_312.videos');
// files/user_id_312/videos/
Returns the interval from a date until the desired date.
until('now', 'next month')->total('days');
// 25
Returns the currently authenticated user, if any.
user()?->name;
// "John Doe"
Returns the end of the week. It supports setting what day the week end.
weekend()->toDateTimeString();
// 2015-07-05 23:59:59
weekend('now')->toDateTimeString();
// 2015-07-13 23:59:59
Returns the end of the week. It supports setting what day the week starts.
weekstart()->toDateTimeString();
// 2015-06-28 00:00:00
weekstart('now')->toDateTimeString();
// 2015-07-06 00:00:00
Returns the key of the option which comparison or callback returns true.
$which = which_of('foo', [0 => 'baz', 1 => 'bar', 2 => 'foo'])
// 2
If the callback returns something truthy, that value will be used.
$which = which_of(
'foo',
['baz', 'bar', 'foo'],
fn($subject, $option) => $subject === $option ? 'cougar' : false)
);
// "cougar"
Returns the date for yesterday.
// 2015-07-12 17:30:00
yesterday();
// 2015-07-11 00:00:00
If you have an idea for a helper, shoot it as an issue. PR with test and good code quality receive priority.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.