ryangjchandler / laravel-helpers
A collection of helper functions that I use across my projects.
Fund package maintenance!
ryangjchandler
Requires
- php: ^8.2
- illuminate/contracts: ^11.0
- spatie/laravel-package-tools: ^1.4.3
Requires (Dev)
- brianium/paratest: ^7.0
- nunomaduro/collision: ^8.1
- orchestra/testbench: ^9.4
- pestphp/pest: ^2.0
- phpunit/phpunit: ^10.0
- spatie/laravel-ray: ^1.23
README
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 ofapp/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 —
in your HTML code when it isn't present. Doing this with regular Blade {{ }}
tags can be annoying though since —
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.