A collection of helper functions that I use across my projects.
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.
selected
Can be used to output a selected
attribute conditionally.
">
<select>
<option value="option" {{ selected($value === 'option') }}>option>
select>
You can also change the attribute that is returned. This is useful when you want to use it on checkboxes.
">
<input type="checkbox" name="remember" {{ selected($remember, 'checked') }} />
authorize
Identical to Laravel's $this->authorize()
method provided by the AuthorizesRequests
trait.
public function index()
{
authorize('viewAny', Post::class);
}
url_shorten
Removes everything before ://
in a URL for use as link text.
{{ url_shorten($url) }}
">
<a href="{{ $url }}">{{ url_shorten($url) }}a>
This is also available as a Str::shortenUrl
method, applied via a macro.
$shortened = Str::shortenUrl($url);
Request::collect()
This behaves exactly the same as Request::all()
, except it returns an instance of \Illuminate\Support\Collection
.
public function store(Request $request)
{
$collection = $request->collect();
$pluckedCollection = $request->collect('name', 'email', 'age');
}
Testing
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.