Search code examples
laraveleloquentlumen

Eloquent not adding automatically s at the end of table name


I have a Lumen app. I just created a new TillSoftware model with this migration file

Schema::create('till_softwares', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string("name", 120);
        $table->timestamps();
    });

But when I just run a simple request like this

$softwareList = TillSoftware::all();

I receive an error telling

Next Illuminate\Database\QueryException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'database.till_software' doesn't exist 

It doesn't seem to add the usual extra s at the end of the table name and trying to make a request to till_software instead of till_softwares. I have more than 20 other models which aren't running just fine, and as far as I know plural of software is softwares.

Am I missing something obvious here? I still can add an extra

protected $table = 'till_softwares';

on my model but I would prefer to understand what I did incorrectly.

Thank you for your help.


Solution

  • So laravel will use Str::plural() to find the plural of the model name to find the table name as this is standard.

    >>> Str::plural('user')
    => "users"
    

    So this is User model into users table

    >>> Str::plural('software')
    => "software"
    

    but the plural of software is software, so that is the table name.

    If you want to use softwares you will need to do as you said

    protected $table = 'till_softwares';
    

    But I would say that using the standard table names would be best, so it should be till_software