How to truncate you strings on a Blade template

Truncate your text using Str::limit on your Blade templates.'

By Alex Vaught - 06 May 2020

Tags: Article

How to truncate you strings on Blade template

The is an easy way that you can use to truncate your excerpt text to use in small containers. You just have to use str_limit for Laravel 4 to 5 or Str::limit helper in Laravel 5.8 and up to limit the number of characters in a string.

// For use with Laravel 4 to 5
-mustaches- str_limit($string, $limit = 150, $end = '...') -mustaches-
// For use with Laravel 5.8 and up.
-mustaches- \Illuminate\Support\Str::limit($string, 150, $end=\'...\') -mustaches-


As you notice we call the \Illuminate\Support\Str on the Blade and add the limit() function to limit our string.


The limit() function accepts three parameters (String, Int, String). The first param is the string variable you are passing, the second param is the string length as an integer that will be used to limit your string in characters. The third is an optional string, that will be appended to the end of your limit string.

-mustaches- Illuminate\Support\Str::limit($string, 20, ' ...') -mustaches-


There are other helper functions for Strings in Laravel that can be useful: