PHPackages                             nedwors/pluralize - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. nedwors/pluralize

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

nedwors/pluralize
=================

Easily and fluently pluralize content within your Laravel apps

5.0.0(9mo ago)53.5kMITPHPPHP ^8.3|^8.4CI passing

Since Nov 28Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/nedwors/pluralize)[ Packagist](https://packagist.org/packages/nedwors/pluralize)[ Docs](https://github.com/nedwors/pluralize)[ RSS](/packages/nedwors-pluralize/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (10)Dependencies (4)Versions (18)Used By (0)

Pluralize - A Laravel string helper
===================================

[](#pluralize---a-laravel-string-helper)

[![Latest Version on Packagist](https://camo.githubusercontent.com/3003ebca9417bcfbc594c6385824f84b50e22507470459e80ba575a4b853159b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e6564776f72732f706c7572616c697a652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/nedwors/pluralize)[![Tests](https://github.com/nedwors/pluralize/workflows/tests/badge.svg)](https://github.com/nedwors/pluralize/workflows/tests/badge.svg)

A Laravel package that provides null-safe, meaningful pluralization of strings.

Go from this...

```
@if($pizzas)

{{ $pizzas->count() }} {{ Str::plural('Pizza', $pizzas->count()) }}

@else

-

@endif

// 2 Pizzas
// -
```

To this...

```
{{ pluralize('Pizza', $pizzas) }}

// 2 Pizzas
// -
```

Nice eh?

No more `count($me)`, `$me->count()` or `$me->total()`... Just pass in your variable and have it counted for you.

No need to repeat `{{ $lemons->count() }} {{ Str::plural('Lemon', $lemons->count()) }}` over and over everywhere in your views. Just a unified format across your app.

No need to think anymore about whether the variable is `null` and what to do if so, just a clean `-`, or [whatever you want](#or).

> The stuff of nightmares...
>
> `Call to a member function count() on null`

Docs
----

[](#docs)

- [Installation](#installation)
    - [Setup](#setup)
- [Introduction](#introduction)
    - [Helper vs Class](#helper-vs-class)
    - [Usage](#usage)
- [Features/API](#features/api)
    - [Pluralize](#pluralize)
    - [This](#this)
    - [From](#from)
    - [Or](#or)
    - [As](#as)
- [Configuration](#configuration)
    - [Default Bindings](#default-bindings)
    - [Specific Bindings](#specific-bindings)
    - [Driver](#driver)

Installation
------------

[](#installation)

You can install the package via composer

```
composer require nedwors/pluralize
```

### Setup

[](#setup)

Minimal, if any, setup is required. The package is ready to work out of the box with sensible defaults.

The defaults are shown below. The 1st comment represents the returned string when `$pizzas` has a count of 10; the 2nd represents when `$pizzas` is null:

```
pluralize('Pizza', $pizzas)

// 10 Pizzas
// -
```

However you are free to [configure](#configuration) these defaults, and fine tune the system further.

Introduction
------------

[](#introduction)

### Helper vs Class

[](#helper-vs-class)

This documentation's examples are based on the helper function `pluralize()`. However, the underlying class can be used just the same for every feature. For instance, the following are equivalent:

```
pluralize('Dog', $dogs, '...')

pluralize('Dog')->from($dogs)->or('...')

Pluralize::this('Dog')->from($dogs)->or('...')
```

### Usage

[](#usage)

The underlying class in the package implements the `Stringable` interface, so in your Blade views it works just by calling the helper function or class.

```
pluralize('Dog', $dogs)
```

Outside your views, do any of the following

```
pluralize('Dog', $dogs)() // Invoke

pluralize('Dog', $dogs)->go() // Call the go() method

(string) pluralize('Dog', $dogs) // Cast the type
```

Features/API
------------

[](#featuresapi)

### Pluralize

[](#pluralize)

Pluralize has 4 main functions:

```
Pluralize::this(...)->from(...)->as(...)->or(...)
```

The `pluralize()` helper function has 4 parameters that map to these functions:

```
pluralize('this', 'from', 'or', 'as')
```

You'll notice that `or` is listed before `as` in the parameter list. This helps with usability for most use cases.

It can be accessed fluently too

```
pluralize(...)->from(...)->as(...)->or(...)
```

### This

[](#this)

The singular form of the string you wish to be pluralized.

```
// The first argument to the helper function

pluralize('Rocket')
```

### From

[](#from)

The count/total/sum you wish to pluralize the string from.

The variable you pass can be an `int`, an `array`, a [`Collection`](https://laravel.com/docs/8.x/collections), a [`LengthAwarePaginator`](https://laravel.com/docs/8.x/pagination#displaying-pagination-results) or a [`Paginator`](https://laravel.com/docs/8.x/pagination#displaying-pagination-results).

```
// The second argument to the helper function

pluralize('Rocket', $rockets)

// Or, as a method

pluralize('Rocket')->from($rockets)
```

> By this point, it's worth noting that you are good to go. Nothing more is required for most uses.
>
> Pluralize does provide extra features for more flexibility. These are detailed below, as well as [configuration](#configuration).

### Or

[](#or)

The string to display if the [count](#from) is `null`.

This is not required. If not provided, it will simply defer to the default `-`.

```
// The third argument to the helper function

pluralize('Rocket', $rockets, '...')

// Or, as a method

pluralize('Rocket', $rockets)->or('...')
```

In addition to providing a `string`, you can pass a `Closure` that will receive the pluralized form of the word.

```
pluralize('Rocket', $rockets)
    ->or(fn($plural) => "Oops, $plural is not defined")

// Oops, Rockets is not defined
```

##### Why would I ever write a `Closure` when I can just type `Oops, Rockets is not defined`?

[](#why-would-i-ever-write-a-closure-when-i-can-just-type-oops-rockets-is-not-defined)

It's true, it's probably unlikely. But at least the power is there if needed. For instance, perhaps you want to grab the `Auth::user()`, or give context with the current time.

However, the power of using a `Closure` really comes when [configuring](#configuration) the package.

### As

[](#as)

The format to display the pluralization.

This is not required. If not provided, it will simply defer to the default `n items`.

The most useful means is declaring this as a `Closure`, which is passed the plural form of the string and the count.

```
// The fourth argument to the helper function

pluralize('Rocket', $rockets, '...', fn($plural, $count) => "$plural: $count")

// Or, probably more usefully, as a method

pluralize('Rocket', $rockets)->as(fn($plural, $count) => "$plural: $count")

// Rockets: 10
```

How about if you wanted something along the lines of `there are 10 Rockets`? When there's 1 `Rocket`, you'll end up with `There are 1 Rocket`... Hmm. Well, the pipe operator is your friend! Simply declare the singular output to the left, the plural to the right.

```
pluralize('Rocket', $rockets)
    ->as(fn($plural, $count) => "There is|are $count $plural")

// There is 1 Rocket
// There are 2 Rockets
```

Now, you can pass a `string` if you really, really want to...

```
pluralize('Rocket', $rockets)
    ->as('Not sure how many, but you have some Rockets')

// Now sure how many, but you have some Rockets
```

...but this is most useful when used with your [configuration](#configuration).

Configuration
-------------

[](#configuration)

You can easily configure different aspects of the package. This is all done via the `Pluralize` class in your service provider.

### Default Bindings

[](#default-bindings)

You can declare the default formats for use in your app by calling `Pluralize::bind()` with no arguments

```
// In your service provider's boot() method
Pluralize::bind()
    ->output(fn($plural, $count) => "$plural: $count")
    ->fallback('...')

// In your view
pluralize('Car', $cars)

// When $cars = null, ...
// When $cars = 10, Cars: 10
```

> The default Output is `n items`. The default Fallback is `-`.

### Specific Bindings

[](#specific-bindings)

You can bind to the word you want to pluralize for specific formatting

```
// In your service provider's boot() method
Pluralize::bind('Hotdog')
    ->output(fn($plural, $count) => "Yum, $count hotdogs!")

// In your view
pluralize('Car', $cars) // 10 Cars
pluralize('Hotdog', $hotdogs) // Yum, 10 hotdogs!
```

You can set an arbitrary key to be referred to at time of render

```
// In your service provider's boot() method
Pluralize::bind('ye-olde')
    ->fallback(fn($plural) => "Thou hath not declared $plural")

// In your view
pluralize('Robot', null) // -
pluralize('DeLorean', null)->or('ye-olde') // Thou hath not declared DeLoreans
```

### Driver

[](#driver)

The package uses the Laravel [`Str::plural()`](https://laravel.com/docs/8.x/helpers#method-str-plural) method for pluralizing the strings passed in. You are free to use your own driver if desired. This would be especially useful for non-english languages.

Write your custom driver implementing the `Pluralization` interface. This defines one method, `run()`, which is passed the singular string and the current count. Here's the current implementation:

```
public function run(string $string, $count): string
{
    return Str::plural($string, $count);
}
```

Then set this as the desired driver in the `boot()` method of your service provider

```
Pluralize::driver(NewDriver::class)
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Sam Rowden](https://github.com/nedwors)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

Laravel Package Boilerplate
---------------------------

[](#laravel-package-boilerplate)

This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com).

###  Health Score

47

—

FairBetter than 93% of packages

Maintenance58

Moderate activity, may be stable

Popularity26

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity80

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~109 days

Recently: every ~225 days

Total

17

Last Release

279d ago

Major Versions

v1.1.2 → 2.0.02021-03-05

v1.1.3 → 2.x-dev2021-11-23

2.0.1 → 3.0.02022-02-21

3.1.0 → 4.0.02024-08-08

4.x-dev → 5.0.02025-09-16

PHP version history (6 changes)v1.0.0PHP ^7.1

2.0.0PHP ^7.4|^8.0

3.0.0PHP ^8.0|^8.1

3.x-devPHP ^8.0|^8.1|^8.2

4.0.0PHP ^8.2

5.0.0PHP ^8.3|^8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/8581f7aef8679c8f7501cd36239817172bd23a8c83804055f41c73b44e93642f?d=identicon)[nedwors](/maintainers/nedwors)

---

Top Contributors

[![nedwors](https://avatars.githubusercontent.com/u/59183434?v=4)](https://github.com/nedwors "nedwors (38 commits)")

---

Tags

pluralizenedwors

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/nedwors-pluralize/health.svg)

```
[![Health](https://phpackages.com/badges/nedwors-pluralize/health.svg)](https://phpackages.com/packages/nedwors-pluralize)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3345.1M337](/packages/psalm-plugin-laravel)[illuminate/pipeline

The Illuminate Pipeline package.

9348.3M267](/packages/illuminate-pipeline)[illuminate/pagination

The Illuminate Pagination package.

10533.5M989](/packages/illuminate-pagination)[erag/laravel-pwa

A simple and easy-to-use PWA (Progressive Web App) package for Laravel applications.

179110.0k](/packages/erag-laravel-pwa)[erag/laravel-lang-sync-inertia

A powerful Laravel package for syncing and managing language translations across backend and Inertia.js (Vue/React) frontends, offering effortless localization, auto-sync features, and smooth multi-language support for modern Laravel applications.

4821.5k](/packages/erag-laravel-lang-sync-inertia)[illuminate/cookie

The Illuminate Cookie package.

224.5M132](/packages/illuminate-cookie)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
