str_plural(): Quickest way to show plural noun

Laravel has a lot of helpers, for example to work with strings or arrays. One of them in particular helps with pluralization. Here’s what I mean.

Imagine a situation where you have to show text “X item(s)”, where if X > 1 then it’s “items”, otherwise “1 item”. How do we do that in Blade with simple IF?

Or a little shorter:

But there’s even shorter way – with function str_plural():

Function str_plural($noun, $count) has two parameters – it checks second parameter $count, and if its value is greater than 1 – it pluralizes the first parameter $noun.

Actually, second parameter is optional – so if you just want to show pluralize form of a noun, you can just do something like this:

It would show ‘children’.

Any more useful helpers that are your favorites in Laravel? Let me know in the comments.

Want more tips and articles like this?
20-50 useful links every week on our weekly newsletter - comes out every Thursday!



2 thoughts on “str_plural(): Quickest way to show plural noun

Leave a Reply

Your email address will not be published. Required fields are marked *