ryangjchandler/laravel-helpers

A collection of helper functions that I use across my projects.

v0.7.0 2024-09-20 15:47 UTC

This package is auto-updated.

Last update: 2024-11-20 16:12:14 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package includes some of the helper functions that I tend to use in all of my projects.

Installation

You can install the package via composer:

composer require ryangjchandler/laravel-helpers

Usage

user

Returns the current user, or null depending on authentication status.

This function assumes that your User model is found inside of app/Models and will not be registered if that class doesn't exist.

$user = user();

route_is

Check if the current route name matches the provided string.

route_is('dashboard.index');

authorize

Identical to Laravel's $this->authorize() method provided by the AuthorizesRequests trait.

public function index()
{
    authorize('viewAny', Post::class);
}

attributes() and @attributes

Laravel 9 introduces new directives for checked, disabled and selected. In some cases though, you might want to output a variety of different attributes using PHP values.

attributes() and the @attributes() directive can help with that:

<button @attributes([
    'disabled' => ! $user->can('click'),
])>
</button>

mdash()

It's quite common to output an &mdash; in your HTML code when it isn't present. Doing this with regular Blade {{ }} tags can be annoying though since &mdash; needs to be output in "raw" mode.

This function uses HtmlString to return a "safe" wrapper around the HTML entity which allows it to be output without being escaped.

{{ $post->published_at?->format('d/m/Y') ?? mdash() }}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.