❤️ We proudly support the community by developing Laravel packages and giving them away for free. If this package saves you time or if you're relying on it professionally, please consider sponsoring the maintenance and development and check out our latest premium package: Inertia Table. Keeping track of issues and pull requests takes time, but we're happy to help!
- PHP 8.1+
- Laravel 10.0
You can install the package via composer:
composer require protonemedia/laravel-mixins
There's no Service Provider or automatic discovery/registration of anything. All features are opt-in.
You can register Blade Directives by calling the directive
method on the class. You can change the name of a directive with the optional first argument.
Note: This directive requires the moneyphp/money
package.
Register the directive, for example by adding it to your AppSerivceProvider
:
ProtoneMedia\LaravelMixins\Blade\DecimalMoneyFormatter::directive();
You can customize the name of the directive and the default currency code:
ProtoneMedia\LaravelMixins\Blade\DecimalMoneyFormatter::directive('decimals', 'EUR');
The first argument of the directive is the amount in cents. The second optional parameter is the currency.
// 0.99
@decimals(99)
// 1.00
@decimals(100)
// 100
@decimals(100, 'XTS')
Note: This directive requires the moneyphp/money
package.
Register the directive, for example by adding it to your AppSerivceProvider
:
ProtoneMedia\LaravelMixins\Blade\IntlMoneyFormatter::directive();
You can customize the name of the directive, the default currency code and the locale:
ProtoneMedia\LaravelMixins\Blade\IntlMoneyFormatter::directive('money', 'EUR', 'nl_NL');
The first argument of the directive is the amount in cents. The optional second parameter is the currency. The optional third parameter is the locale.
// € 0,99
@money(99)
// € 1,00
@money(100)
// US$ 1,00
@money(100, 'USD')
// 1 000,00 $US
@money(100, 'USD', 'fr')
Note: This command requires the spatie/laravel-sitemap
package.
You can register the command by adding it to your App\Console\Kernel
file, or by calling the register
method on the class.
ProtoneMedia\LaravelMixins\Commands\GenerateSitemap::register();
You can also set a custom signature:
ProtoneMedia\LaravelMixins\Commands\GenerateSitemap::register('generate-sitemap');
It generates a sitemap of your entire site and stores in in the public
folder as sitemap.xml
.
php artisan sitemap:generate
Passes if the value matches the password of the authenticated user.
$rule = new ProtoneMedia\LaravelMixins\Rules\CurrentPassword;
As of Laravel 9, this validation rule is built-in.
Extension of the Dimensions rule with a margin
option. Handy when you're working with ratios with repeating decimals.
use ProtoneMedia\LaravelMixins\Rules\DimensionsWithMargin;
$rule = DimensionsWithMargin::make()->ratio(20 / 9)->margin(1),
Verifies if the URL matches the given hosts.
use ProtoneMedia\LaravelMixins\Rules\Host;
$rule = Host::make(['facebook.com', 'fb.me']);
Verifies if the given key or index exists in the array.
use ProtoneMedia\LaravelMixins\Rules\InKeys;
$rule = new InKeys(['laravel' => 'Laravel Framework', 'tailwindcss' => 'Tailwind CSS framework']);
// same as
use Illuminate\Validation\Rules\In;
$rule = new In(['laravel', 'tailwindcss']);
Passes if the values contains no more words than specified.
use ProtoneMedia\LaravelMixins\Rules\MaxWords;
$rule = MaxWords::make(250);
Passes if the URL is valid, even without a scheme.
$rule = new ProtoneMedia\LaravelMixins\Rules\UrlWithoutScheme;
You can add new method by using the mixins.
Str::mixin(new ProtoneMedia\LaravelMixins\String\Compact);
$string = "Hoe simpeler hoe beter. Want hoe minder keuze je een speler laat, hoe groter de kans dat hij het juiste doet.";
// Hoe simpeler hoe beter. Want hoe ... de kans dat hij het juiste doet.
echo Str::compact($string);
It has an optional second argument to specify the length on each side. With the optional third argument, you can specify the sepeator.
// Hoe simpeler hoe - het juiste doet.
echo Str::compact($string, 16, ' - ');
Converts a filesize into a human-readable version of the string.
Str::mixin(new ProtoneMedia\LaravelMixins\String\HumanFilesize);
$size = 3456789;
// '3.3 MB'
Str::humanFilesize($size));
Note: This macro requires the html2text/html2text
package.
Converts HTML to plain text.
Str::mixin(new ProtoneMedia\LaravelMixins\String\Text);
$html = "<h1>Protone Media</h1>";
// Protone Media
Str::text($html);
Prepends https://
if the scheme is missing from the given URL.
Str::mixin(new ProtoneMedia\LaravelMixins\String\Url);
$url = "protone.media";
// https://protone.media
Str::url($url);
Converts seconds to a 'mm:ss' / 'hh:mm:ss' format.
Str::mixin(new ProtoneMedia\LaravelMixins\String\SecondsToTime);
Str::secondsToTime(10); // 00:10
Str::secondsToTime(580); // 09:40
Str::secondsToTime(3610); // 01:00:10
// force 'hh:mm:ss' format, even under an hour:
Str::secondsToTime(580, false); // 00:09:40
Note: Requires the symfony/process
package.
Regenerates the PDF content with Ghostscript.
$ghostscript = new ProtoneMedia\LaravelMixins\Pdf\Ghostscript;
$regeneratedPdf = $ghostscript->regeneratePdf(
file_get_contents('/uploads/invoice.pdf')
);
You can specify the path of the ghostscript
binary as well:
$ghostscript = new Ghostscript('gs-binary');
Add the ConvertsBase64ToFiles
trait and base64FileKeys
method to your form request.
use Illuminate\Foundation\Http\FormRequest;
use ProtoneMedia\LaravelMixins\Request\ConvertsBase64ToFiles;
class ImageRequest extends FormRequest
{
use ConvertsBase64ToFiles;
protected function base64FileKeys(): array
{
return [
'jpg_image' => 'Logo.jpg',
];
}
public function rules()
{
return [
'jpg_image' => ['required', 'file', 'image'],
];
}
}
Now you can get the files like regular uploaded files:
$jpgFile = $request->file('jpg_image');
// Logo.jpg
$jpgFile->getClientOriginalName();
This trait supports nested data as well. You can either reference the keys by a nested array, or with a dotted notation:
class ImageRequest extends FormRequest
{
use ConvertsBase64ToFiles;
protected function base64FileKeys(): array
{
return [
'company.logo' => 'Logo.jpg',
'user' => [
'avatar' => 'Avatar.jpg',
],
];
}
}
Want to know more about this trait? Check out the blog post.
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
Inertia Table
: The Ultimate Table for Inertia.js with built-in Query Builder.Laravel Blade On Demand
: Laravel package to compile Blade templates in memory.Laravel Cross Eloquent Search
: Laravel package to search through multiple Eloquent models.Laravel Eloquent Scope as Select
: Stop duplicating your Eloquent query scopes and constraints in PHP. This package lets you re-use your query scopes and constraints by adding them as a subquery.Laravel FFMpeg
: This package provides an integration with FFmpeg for Laravel. The storage of the files is handled by Laravel's Filesystem.Laravel MinIO Testing Tools
: Run your tests against a MinIO S3 server.Laravel Paddle
: Paddle.com API integration for Laravel with support for webhooks/events.Laravel Task Runner
: Write Shell scripts like Blade Components and run them locally or on a remote server.Laravel Verify New Email
: This package adds support for verifying new email addresses: when a user updates its email address, it won't replace the old one until the new one is verified.Laravel XSS Protection
: Laravel Middleware to protect your app against Cross-site scripting (XSS). It sanitizes request input, and it can sanatize Blade echo statements.
If you discover any security related issues, please email pascal@protone.media instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.