axn / laravel-extension
Includes a set of useful tools for the Laravel framework.
Installs: 2 625
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
- composer/semver: ^3.0.0
- forxer/generic-term-translations-for-laravel: ^1.1.0
- laravel/framework: ^8.0 || ^9.0 || ^10.0
Requires (Dev)
- driftingly/rector-laravel: >=0.30.0
- laravel/pint: ^1.13.9
- rector/rector: >=0.19.1
- dev-master
- 9.1.0
- 9.0.4
- 9.0.3
- 9.0.2
- 9.0.1
- 9.0.0
- 8.1.0
- 8.0.0
- 7.7.2
- 7.7.1
- 7.7.0
- 7.6.0
- 7.5.2
- 7.5.1
- 7.5.0
- 7.4.0
- 7.3.0
- 7.2.0
- 7.1.1
- 7.1.0
- 7.0.1
- 7.0.0
- 6.9.0
- 6.8.0
- 6.7.0
- 6.6.0
- 6.5.0
- 6.4.0
- 6.3.0
- 6.2.0
- 6.1.0
- 6.0.1
- 6.0.0
- v5.x-dev
- 5.4.2
- 5.4.1
- 5.4.0
- 5.3.0
- 5.2.0
- 5.1.1
- 5.1.0
- 5.0
- v4.x-dev
- 4.2.0
- 4.1.0
- 4.0.2
- 4.0.1
- 4.0.0
- 3.2.0
- 3.1.0
- 3.0.1
- 3.0.0
- 2.2.1
- 2.2.0
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.0
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.1
- 1.0.0
- dev-develop
This package is auto-updated.
Last update: 2024-01-17 13:07:08 UTC
README
Includes a set of useful tools for the Laravel framework.
Installation
With Composer :
composer require axn/tool-kit-for-laravel
To use some of these tools you must have correctly installed the package forxer/generic-term-translations-for-laravel already prerequisite by this package (therefore present).
Use the locales publisher of Laravel Lang to add/update/reset or remove translations:
- If you have never used Laravel Lang: add locales
- If you are already using Laravel Lang: just update the locales
Helpers
carbon()
Create a Carbon instance from a date string, a DateTime instance or a timestamp.
/** * Create a Carbon instance from a date string, a DateTime instance or a timestamp. * * @param \DateTime|int|string|null $date * @param string|null $fromFormat * @param \DateTimeZone|string|null $tz * @return \Illuminate\Support\Carbon * */ function carbon($date = null, $fromFormat = null, $tz = null)
Here are some examples.
Using Carbon:
use Carbon\Carbon; $date = Carbon::now(); $date = Carbon::now('Europe/Paris'); $date = Carbon::createFromFormat('Y-m-d H:i', '2018-06-18 09:30'); $date = Carbon::createFromFormat('Y-m-d H:i', '2018-06-18 09:30', 'Europe/Paris'); $date = new Carbon('Thursday, June 18 2015 9:30:00'); $date = new Carbon('Thursday, June 18 2015 9:30:00', 'Europe/Paris'); $date = Carbon::createFromTimestamp(1434619800)
Equivalents using helper:
$date = carbon(); $date = carbon(tz: 'Europe/Paris'); $date = carbon('2018-06-18 09:30', 'Y-m-d H:i'); $date = carbon('2018-06-18 09:30', 'Y-m-d H:i', 'Europe/Paris'); $date = carbon('Thursday, June 18 2015 9:30:00'); $date = carbon('Thursday, June 18 2015 9:30:00', tz: 'Europe/Paris'); $date = carbon(1434619800) $date = carbon(1434619800, tz: 'Europe/Paris')
collect_models()
Create a collection of Eloquent models.
/** * Create an Eloquent collection of Eloquent models. * * @param array $models * @return EloquentCollection */ function collect_models(array $models)
str_html()
Create an Illuminate\Support\HtmlString
instance.
$str = '<a>An HTML string</p>'; $htmlString = str_html($str); // Alias of $htmlString = new Illuminate\Support\HtmlStringHtmlString($str);
linebreaks()
Convert all line-endings to UNIX format.
Replace "\r\n"
and "\r"
by "\n"
nl_to_p()
Convert new lines into HTML paragraphs <p>
.
$str = "a text with \n new lines \n\n again \n\n\n and again"; nl_to_p($str); // <p>a text with <br> new lines </p><p> again </p><p> and again</p>
nl_to_br()
Alias of native PHP function nl2br()
.
$str = "a text with \n new lines \n\n again \n\n\n and again"; nl_to_br($str) // a text with <br> new lines <br><br> again <br><br><br> and again
number_formated()
Returns a number in current language format.
$number = '123456789.101112'; $numberFormated = number_formated($number, 2); // fr: 123 456 789,10 // en: 123,456,789.10
number_fr()
Returns a number in french format.
compute_dec_to_time()
Decimal to time calculation, return an array with hours, minutes and seconds.
$number = '1.75'; $time = compute_dec_to_time($number); // [ // 'hours' => 1.0, // 'minutes' => 45.0, // 'seconds' => 0, // ]
convert_dec_to_time()
Decimal to time conversion. Output can be changed with sprintf
format.
$number = '1.75'; $time = convert_dec_to_time($number); // 01:45:00 $time = convert_dec_to_time($number, '%sh%s'); // 1h45 $time = convert_dec_to_time($number, '%2$s:%3$s'); // 45:00
human_readable_bytes_size()
Convert a bytes size into a human readable localized size.
$size = human_readable_bytes_size(2048); // fr: 2 ko // en: 2 kB $size = human_readable_bytes_size(2048*1024); // fr: 2 Mo // en: 2 MB $size = human_readable_bytes_size(2048*1024*10000, 2); // fr: 19,53 Go // en: 19.53 GB
mime_type_to_fa5_class()
Return a font awesome file icon class for specific MIME Type.
trans_ucfirst()
Translate the given message with first character uppercase.
is_valid_model()
Indicates whether the model class is instantiable and is an instance of Illuminate\Database\Eloquent\Model
.
semverToId()
Transforms a semver version number into a numeric identifier. Please note: does not take into account "pre-releases" (RC, beta, etc.)
$phpVersion = "8.2.14"; $phpVersionId = semverToId($phpVersion); // 80214 $laravelVersion = " 10.38.2"; $laravelVersionId = semverToId($laravelVersion); // 103802
This is useful for optimizing comparisons, searches and sorting in a database on numeric rather than text columns.
Blade directives
@nltop()
Convert new lines into HTML paragraphs <p>
.
@nltop ("a text with \n new lines \n\n again \n\n\n and again")
Displays:
<p>a text with <br> new lines </p><p> again </p><p> and again</p>
@nltobr()
Convert new lines into HTML <br>
@nltobr ("a text with \n new lines \n\n again \n\n\n and again")
Displays:
a text with <br> new lines <br><br> again <br><br><br> and again
Components
Add an indicator for required field
To display a required field marker (e.g. in a label tag):
<x-required-field-marker />
Displays:
<span class="required-field-marker"> *<span>required</span> </span>
You can change the default symbol "*" (an asterisk) by the marker symbol of your choice:
<x-required-field-marker :symbol="⚠" />
You can style it for example like this:
.required-field-marker { color: #da1313; } .required-field-marker > span { /* Bootstrap styles of .visually-hidden class */ position: absolute !important; width: 1px !important; height: 1px !important; padding: 0 !important; margin: -1px !important; overflow: hidden !important; clip: rect(0, 0, 0, 0) !important; white-space: nowrap !important; border: 0 !important; }
In your forms you can indicate the required fields for example in this way:
{!! trans('misc.info_required_fields'); !!} <x-required-field-marker />