Hey Laravel users — are you using Plural / Singular!?

Introduction

Laravel comes packed with tons of handy string manipulation helpers. Among them, Str::plural() and Str::singular() are super useful when you need to follow Laravel’s naming conventions for database tables and models!

Today, I’l…


This content originally appeared on DEV Community and was authored by yone3

Introduction

Laravel comes packed with tons of handy string manipulation helpers. Among them, Str::plural() and Str::singular() are super useful when you need to follow Laravel's naming conventions for database tables and models!

Today, I'll show you how to use these methods and share some practical examples ✨

Basic Usage

Str::plural() - Converting to Plural Form

Converts singular English words to their plural forms.

use Illuminate\Support\Str;

// Basic examples
Str::plural('user');     // users
Str::plural('person');   // people
Str::plural('child');    // children
Str::plural('ox');       // oxen

Str::singular() - Converting to Singular Form

Converts plural English words to their singular forms.

Str::singular('users');    // user
Str::singular('people');   // person
Str::singular('children'); // child
Str::singular('oxen');     // ox

Practical Examples

1. Dynamic Table Name Generation

$modelName = 'User';
$tableName = Str::plural(Str::snake($modelName));
// Returns 'users'

2. Dynamic Message Generation

$count = 5;
$item = 'item';
$message = "You have {$count} " . Str::plural($item, $count);
// "You have 5 items"

Live Demo

I've created a demo page where you can try it out!

🔗 Singular/Plural Conversion Sample

On this page, you can test singular/plural conversion with various English words.

Specifically, the Controller handles the conversion like this:

$word = $request->input('word');
$singular = \Illuminate\Support\Str::singular($word);
$plural = \Illuminate\Support\Str::plural($word);

Since it's deployed on Render, the first access might take a bit longer to load.

Important Notes

  • These methods cover basic English pluralization rules, but they don't handle all irregular forms
  • For more complex conversions, consider using the Illuminate\Support\Pluralizer class directly
  • If you need custom rules, you can register your own conversion rules in a service provider

Conclusion

Str::plural() and Str::singular() are incredibly useful helper methods when developing with Laravel's conventions. Try using them when dynamically generating table names or messages!

Feel free to try out various words on the demo page! ♪


This content originally appeared on DEV Community and was authored by yone3


Print Share Comment Cite Upload Translate Updates
APA

yone3 | Sciencx (2025-10-25T07:42:41+00:00) Hey Laravel users — are you using Plural / Singular!?. Retrieved from https://www.scien.cx/2025/10/25/hey-laravel-users-are-you-using-plural-singular-2/

MLA
" » Hey Laravel users — are you using Plural / Singular!?." yone3 | Sciencx - Saturday October 25, 2025, https://www.scien.cx/2025/10/25/hey-laravel-users-are-you-using-plural-singular-2/
HARVARD
yone3 | Sciencx Saturday October 25, 2025 » Hey Laravel users — are you using Plural / Singular!?., viewed ,<https://www.scien.cx/2025/10/25/hey-laravel-users-are-you-using-plural-singular-2/>
VANCOUVER
yone3 | Sciencx - » Hey Laravel users — are you using Plural / Singular!?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/25/hey-laravel-users-are-you-using-plural-singular-2/
CHICAGO
" » Hey Laravel users — are you using Plural / Singular!?." yone3 | Sciencx - Accessed . https://www.scien.cx/2025/10/25/hey-laravel-users-are-you-using-plural-singular-2/
IEEE
" » Hey Laravel users — are you using Plural / Singular!?." yone3 | Sciencx [Online]. Available: https://www.scien.cx/2025/10/25/hey-laravel-users-are-you-using-plural-singular-2/. [Accessed: ]
rf:citation
» Hey Laravel users — are you using Plural / Singular!? | yone3 | Sciencx | https://www.scien.cx/2025/10/25/hey-laravel-users-are-you-using-plural-singular-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.