Patrick
Patrick

Reputation: 351

Laravel custom inflection

I want to add a custom inflection to Laravel. In Rails this wasn't that hard to do, but I cannot seem to find an answer on how to do it in Laravel.

How do I add my own inflection to Laravel?

Upvotes: 0

Views: 817

Answers (3)

Lucas Pandolfo
Lucas Pandolfo

Reputation: 21

This no longer works for current Laravel. Now you have to write:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Doctrine\Common\Inflector\Inflector;

class PluralizationServiceProvider extends ServiceProvider {

    public function register()
    {
        Inflector::rules('plural', ['irregular' => ['octopus' => 'octopi']]);
    }

}

Upvotes: 2

Sergey Harini
Sergey Harini

Reputation: 15

It is also necessary add the providers folder to your composer autoload.

"autoload": {
    "classmap": [
        ...
        "app/providers"
    ]
},

Upvotes: 0

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87779

Probably doing:

Illuminate\Support\Pluralizer::$irregular['subscribe'] = 'subscribes';

Best place to load it is a Service Provider:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Pluralizer;

class PluralizationServiceProvider extends ServiceProvider {

    public function register()
    {
        Pluralizer::$irregular['octopus'] = 'octopi';
    }

}

And load it in your app/config/app.php:

'App\Providers\PluralizationServiceProvider', 

Upvotes: 2

Related Questions