plural helper method will attempt to return a plural version of the given
$value. It does this through a series of specialized internal functions, that will not be covered in great detail here. This helper method will apply general rules to strings ending in common character sequences, handle special cases and will not attempt some very specific strings. The change in a word, such as to show tense, or the number of items, is called an inflection.
Previous versions of Laravel managed word inflections itself. Starting with version 5, Laravel now uses the doctrine/inflector package. This package is used by the
Illuminate\Support\Pluralizer class, which is utilized by both the
plural and the
Te signature of the
plural method is:
public static function plural( $value, $count = 2 );
The method takes the
$value that should be pluralized, and the
$count of the items. If the
$count is equal to
1, the original
$value is returned.
use Illuminate\Support\Str; // cow echo Str::plural('cow', 1); // cows echo Str::plural('cows'); // person echo Str::plural('person', 1); // people echo Str::plural('person');
Assuming the following messages array:
// The user's messages. $messages = [ 'You have a meeting on Saturday.', 'Your inbox is 90% full', 'Do not eat yellow snow' ];
We could display the number of messages to the user like so:
use Illuminate\Support\Str; $messageCount = count($messages); echo 'You have '.$messageCount.' unread '. Str::plural('message',$messageCount);
If the user had one message, the above example would output:
You have 1 unread message.
$messages array has
3 items in it, the user would see this:
You have 3 unread messages.
plural Special Cases
Any words, or word endings, in the following table will not be affected by the
plural method. This is either because the resulting word is already plural, or because there is no inflection available. Word endings are denoted by the prefix
The following table lists the words that are not inflected by the
plural method. This is either because the resulting word is already pluralized, or because there are no clear inflection rules available, or they handled separately (see the
plural Special cases table).