Str::singular

Converts an English word to its singular form using Laravel's inflector.

Signature
Str::singular(string $value): string
  • Added in Laravel 4.0

Parameters

ParameterTypeDescription
$$valuestringThe word to singularise. Only the final word of a multi-word or delimited string is inflected.

Return value

stringThe singular form of the given word, with the original capitalisation of the last word preserved.

Changelog

  • PHP 9.xThe pluralizer moved to Doctrine Inflector 2.x, which changed a handful of edge-case results such as irregular Latin plurals.

Str::singular runs a word through Laravel's pluralizer, the same component that decides an Eloquent model's default table name. It is rule based, not a dictionary lookup, with an irregular list layered on top: cars becomes car, children becomes child, matrices becomes matrix.

The place you actually want it is metadata work — turning a table name into a model name, a collection key into a label, a route segment into a resource name. It is the inverse partner of Str::plural, and the two round-trip for regular words.

What trips people up

  • Only the last word is inflected. user_roles becomes user_role, which is what you want, but it also means a sentence is untouched except at its tail.
  • Uncountable words are returned as-is: news, equipment, sheep, data. Passing an already-singular word is therefore usually safe but not guaranteed to be.
  • Capitalisation of the result is matched to the input, so Categories yields Category, not category.
  • The rules are English. Other languages need their own handling; this is not a localisation helper.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Derive a model class name from a table name, as in Str::studly(Str::singular('blog_posts')).
  • Label a UI counter correctly when a filtered result set contains exactly one row.
  • Normalise a plural route segment such as /invoices into a resource name for a policy lookup.

Frequently asked questions

Does Str::singular work with languages other than English?

No. The pluralizer applies English rules plus an irregular-word list, so passing French or Spanish nouns produces nonsense. Use translation files with Laravel's pluralisation syntax for localised copy.

Why does Str::singular return the same word I passed in?

The word is on the uncountable list — news, equipment, sheep, data and similar words have no distinct singular form and are deliberately returned untouched.

How do I get a model class name from a table name?

Combine the two helpers: Str::studly(Str::singular('blog_posts')) gives 'BlogPost'. Singularise first, then convert case, because studly() alone would leave the trailing 's'.

More examples

Table name to model class name

Prints BlogPost, OrderItem, Person, Medium and Status. Four of the five are exactly what Laravel's own naming convention expects. The fifth, 'media' becoming 'Medium', is the reminder that the inflector applies Latin rules and that a hand-written $table property is the fix when it guesses wrong.

Example · php
<?php

use Illuminate\Support\Str;

$tables = ['blog_posts', 'order_items', 'people', 'media', 'statuses'];

foreach ($tables as $table) {
    echo str_pad($table, 14), '=> App\\Models\\', Str::studly(Str::singular($table)), PHP_EOL;
}

Round-tripping against Str::plural

Line one prints "geese", showing the round trip holds. Line two prints "Mouse Analysis vertex" and shows that capitalisation follows the input word by word. Line three prints "comment / comments" — for UI counters Str::plural with a count argument is usually a better fit than calling singular() conditionally.

Example · php
<?php

use Illuminate\Support\Str;

// Regular and most irregular words survive a round trip.
echo Str::plural(Str::singular('geese')), PHP_EOL;

// Irregular singulars, resolved individually.
echo Str::singular('Mice'), ' ', Str::singular('Analyses'), ' ', Str::singular('vertices'), PHP_EOL;

// Pluralising with a count keeps the singular when the count is one.
echo Str::plural('comment', 1), ' / ', Str::plural('comment', 4), PHP_EOL;

Discussion

  • Be the first to comment on this lesson.