Skip to content

Instantly share code, notes, and snippets.

@JHWelch
Last active March 18, 2021 20:28
Show Gist options
  • Save JHWelch/8579847ce6bfe87fe469b29309e2be23 to your computer and use it in GitHub Desktop.
Save JHWelch/8579847ce6bfe87fe469b29309e2be23 to your computer and use it in GitHub Desktop.
Laravel Nova Text Field Limit Macro
<?php
/*
* Text Field Limit Macro
* Implementation of Str::limit Helper.
* Truncates a string after $limit characters and adds $end
* Can be added to NovaServiceProvider in boot()
*/
\Laravel\Nova\Fields\Text::macro('limit', function ($limit = 50, $end = '...') {
$this->displayUsing(
function ($text) use ($limit, $end) {
return \Illuminate\Support\Str::limit($text, $limit, $end);
}
);
return $this;
});
/*
* Example
*/
Text::make('First Name')
->sortable()
->rules('required')
->limit(20, '->'),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment