Snippets
How to use str_plural() function in laravel
Sometimes, you may need to make some words plural based on the number counting. For example, you want to tell how many total users. If the total user is 1
, then it should be user
. On the other hand, if users are more than 1, then it should be x users
.
In that situation, instead of using if-else
by counting, we can use a built-in function in laravel, that called str_plural()
.
str_plural()
expect two parameters. The first parameter, the word you want to make it plural, and the second parameter are the numbers, that help to take decision either plural or singular.
Here is an example-
$usersCount = 1;
echo str_plural('user', $usersCount); // user
// More than 1 user.
$usersCount = 6;
echo str_plural('user', $usersCount); // users
Based on the passing parameter in the second argument, it will return singular or plural.