In this episode, we dive into the new SpellOrdinal
method that's been added to Laravel's NumberHelper
class. If you've ever needed to turn numbers like 1, 2, or 3 into their spelled-out ordinals—like "first," "second," or "third"—this is going to make your life way easier. We start off by looking at how the method works in practice, doing a quick demo of converting numbers to their spelled-out ordinals, which is super handy if you're formatting things for reports or user interfaces.
Then we peek under the hood to see how this is actually implemented. You might expect Laravel to have some huge map or complex logic, but surprisingly, it all relies on PHP's built-in NumberFormatter
class. We walk through this implementation and show how you could do it yourself in plain PHP, though it's much nicer and cleaner now that Laravel wraps it up in a neat helper.
We also cover how you can easily switch locales (like US or UK English) if you need your ordinals spelled out a certain way. All in all, this addition to Laravel is a simple quality-of-life improvement that'll save you from writing your own logic or using clunky workarounds whenever you need ordinal numbers as words. Super useful and really straightforward to use in your projects!