Laravel assumes the database table is the plural form of the model name

前端 未结 4 1496
南笙
南笙 2021-01-11 14:09

By default, Laravel is assuming that the database table is the plural form of the model name. But what if my table name is \"news\" and i still want to use this feature? sho

4条回答
  •  死守一世寂寞
    2021-01-11 14:43

    Laravel uses a "standard set" rule that defines:

    • A Model is a single instance of a record
    • A Collection is one or more records

    Therefore it assumes that a table is a collection of records.

    The nomenclature has a problem when it clashes with various features / gotchas of human languages. For example, what if I have a Model called Sheep? That does mean my Database Table should be called "Sheeps"?

    It's up to the developer to avoid "Gollum/Smeagol" syntax. Indeed, you wouldn't want a table called "Newses" as much I'd like to end up with a table called "Sheeps".

    Ultimately, I construct Migrations with:

    sudo php artisan make:migration create_sheep_table --create=sheep
    

    As for Models, you'll notice in the documentation that they have a different table name for "Flights" called "my_flights"

    https://laravel.com/docs/master/eloquent#defining-models

    Again, it's up to the developer / DB manager to make decisions on naming conventions that make sense in an application context.

提交回复
热议问题