How to limit the length of a string in Laravel

How to limit the length of a string in Laravel

The length of a string can be limited in a variety of ways. You might do it with PHP, JavaScript, or CSS, for instance.

Moreover, Laravel offers a helpful aid to make this simple. We'll use Illuminate\Support\Str Str class.

if you need to install laravel then please install laravel project and configure it.

You can use the following to directly limit the length of a string in your blade files:


 {{ Str::limit($string, 30) }}


As this is a global "helper" PHP function included with Laravel out of the box, you don't need to import the namespace.

Simply replace $string with your real string, and 30 with the maximum number of characters you want to allow for your string.


Using model to limit string 


 ...
use Illuminate\Support\Str;

class User
{
const length = 30;

protected $fillable = [
..., 'about'
];

public function strLimit()
{
return Str::limit($this->about, $this->length);
}
}

Just call this method into your blade to convert text to string limit.

Hope this article will help you to  limit the length of a string in laravel.