Tricks

Estimated reading time

Jan 11, 2023
Arthur Minasyan
Form builder

The estimated reading time can help the reader estimate how long the user can read the content to completion.

class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
Str::macro('readingMinutes', function($subject, $wordsPerMinute = 200) {
return intval(ceil(Str::wordCount(strip_tags($subject)) / $wordsPerMinute));
});
}
}
Textarea::make('field')
->hint(function (?string $state) {
$readingMinutes = Str::readingMinutes($state);
 
return $readingMinutes . ' min read';
})
->lazy()

No comments yet…